views:

309

answers:

5

Suppose that I have an ASP.NET page. In the page load event handler, I open a database connection and do some processing. But after the processing is done, I don't close the connection explicitly by calling the CLOSE method of the connection object.

Now when the page processing at the server side is finished, the GC will dispose all the variables in my page, and also, the connection object too. But when it is disposed, does the connection that was opened previously is automatically closed? I mean, when GC disposes the connection object, does it automatically close the connection that was established with the database server; or it simply dispose the connection object, and the connection at the database is remained open, until the connection timeout occurs at the database and then the database server closes the connection by itself?

+1  A: 

You should use using blocks, then you won't have to ask the question:

using (var conn = new SqlConnection(connectionString))
{
    using (var cmd = new SqlCommand(commandText, conn))
    {
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read()) { /* ... */ }
        }
    }
}
John Saunders
If I use the using block, then will the connection at the database be closed by the gcc??
Night Shade
No. The connection will be closed at the end of the using block, even if an exception is thrown within the block.
John Saunders
Actually, the connection with the database will normally not be closed, but it is simply returned to the connection pool. But when not disposing (or closing) a connection properly, you're exhausting the pool and will eventually get a connection pool timeout exception.
Steven
FYI: conn.Open() is missing in your code... you should not call ExecuteReader before opening the connection.
sebastian
+3  A: 

Connection is remained open. If you have lots of pageviews, and lots open connections, you can get 500 error.

x2
+5  A: 

The MSDN documentation is pretty clear about this:

If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose. Close and Dispose are functionally equivalent.

Either use the using blocks to have it disposed automatically, or explicitly .Close() it. The using blocks are preferred.

By leaving connections open your application may eventually run out of connections when new requests are attempted, resulting in errors. I've faced such a problem in an application I was debugging. The original developers failed to close the connections explicitly on a few pages and traffic was high enough that users started getting errors. I wrapped the offending connections in a using block and the problem went away.

Ahmad Mageed
Funny thing is, that there are a couple of differences between Close and Dispose on the SqlConnection class. Besides of course the obvious difference of being able to reopen the connection again after calling Close, the SqlConnection class is not deregistered from the finalizer queue (by calling GC.SuppressFinalize(this)) during a call to close. This increases the pressure on the GC. IMO this is actually a bug in the SqlConnection class.
Steven
+3  A: 

Your connections won't be closed until after (not when) your page object is finalized, and that might be a while. It would be very easy to max out the number of available connections and start getting errors.

Joel Coehoorn
+1  A: 

your connection won't be closed unless chosen by GC and if you have many visitors resulting in lots of connections then this may be horrible. Also if you try to open an opened connection it will throw error so you have to check for that better is either write it in using block or close the connection yourself.

Vinay Pandey