VB.NET / ADO.NET Suggestions

Posted on 2006-11-10
Last Modified: 2008-01-09
I have an VB.NET 2003/2005 program that is a report server.  It's currently using classic ADO and has no problems... when I stress test it runs select statements every 3 seconds to check a queue on a connection that's global and never closed (if it happens to lose connection, it reconnects).  

Now, I've updated my code to use ADO.NET's SqlConnection, SqlCommand and SqlDataReader (used to use ADODB.Connection and ADODB.Recordset).  Same theory, I create a global connection and set MultipleActiveResultSets=True so the connection can handle multiple data readers (using SQL Server 2005).  The problem is, now the program crashes under heavy load with a not enough system resources error (when there is plenty of RAM left and plenty of disk space left)... I can only surmise this is a leak of some sort.

I believe I'm disposing of all of my commands and then setting them equal to nothing... the connection however is constant so I'm not disposing of it at all unless it should lose connection for some reason, then it re-establishes.  I've tried with and without connection pools...  My brain is fried, I need suggestions to get me back on track.  

I could try opening and closing a connection every three seconds when it runs the query but I thought it would be more effiecient if I didn't, if that assumption is incorrect, also let me know.  I'm putting 500 on this because my brain hurts, it's Friday and I'm going to obsess over this until I figure it out.
Question by:IUFITS
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 17917773
1.)  Does the order I dispose in things make a difference?
2.)  Is it better to close and re-establish with each query, every 3 seconds?
3.)  Is the global connection causing this leak somehow?

Additional Code Info:

    '  Conn is then opened in a timer event if it's not established... that event also re-establishes the connection if it's lost
    Public ConnectionString As String = "server=<server>;database=<db>;MultipleActiveResultSets=True;uid=<user>;pwd=<password>"
    Public Conn As New SqlConnection(ConnectionString)

     ' These are my two SQL Command objects
     Dim SqlCommand1 As New SqlCommand("", Conn)
     Dim SqlCommand2 As New SqlCommand("", Conn)

     ' This is how I dispose and remove them
     SqlCommand1 = Nothing
     SqlCommand2 = Nothing
     Dr = Nothing
LVL 13

Accepted Solution

iHadi earned 500 total points
ID: 17918513

When you use Dispose method or when you dereference an object by setting its value to nothing, it stays in memroy until the garbage collector (GC) scans the memory for unused objects and deletes them. You cannot predict when the GC will scan the memory. The GC decides so in a way to optimize performance and resources, so your objects that you disposed might stay in memory from milliseconds to a few days or more depending on the resources and other issues regarding your system. To force the GC to scan the memory and clean it you can use: GC.Collect(). Be carefull though where to use this method because it can cause problems if implemented badly like reducing performance or even raising exceptions in some cases.

Database Connection objects have a lot of overhead, and it takes some time and processor muscles to establish a connection so disconnecting and reconnecting has some performance impact. Instead ADO.NET provides Connection pooling.

The idea behind connection pooling is simple - instead of incurring a large amount of overhead each time a connection to database server is established and closed, once a connection has been opened, it remains open for the lifetime of the process, available to be used again if needed. Pooling database connections can significantly enhance the performance and scalability of your application. The .NET data providers automatically pool connections for you. However, to take maximum advantage of these, you should take note of the following:

- Connections are only pooled if they have the same connection string; so ensure this is always constant.
- When finished with a SqlConnection object, call Dispose() or Close() to release the connection back to the pool.
- In order to keep the maximum number of connections available, you should keep connections open for a short as period as possible - remembering that thanks to connection pooling, re-opening a database connection will incur little overhead.

For more information on connection pooling, take a look at this MSDN page:


Expert Comment

by:Jayadev Nair
ID: 17921471
DataReaders SHOULD be Closed & Disposed. You are doing so right?


Author Comment

ID: 17921796
I'll triple check and make sure that my data readers are being closed and disposed of.  It always crashes on the same line where it loads a crystal report but the reason I don't think that is the problem is because that code hasn't changed and the old engine had no memory issues... the only thing that has changed are the database connections from ADO to ADO.NET.  

iHadi - You brought up something interesting... I was calling gc.collect for reasons I can't remember and it's never been an issue in the past (I would call it during non-report genereating time)...  I wonder if now that I've changed to ADO.NET if it's behaving differently with the new objects... I'll try and comment it out and see how it performs.

The odd thing is that when it says the system it out of resources, the executables aren't really overly large and there's typically plenty of system memory left... I'll post the statistics that I log at crash time (logged the systems virtual and physical memory used and total).

Expert Comment

ID: 17928603
you can also try to close the connection when you finished loading the record.


Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question