views:

561

answers:

4

Earlier today I've found a bug in one of our projects - there is a conenction with database that is never closed, i mean the Close() method is never called. However, when I close the application, the connection is closed (checked in sql management studio many times). Why?

+4  A: 

The connection will close when the application exits. Read up on SqlConnection's Finalize. From MSDN's documentation for Object.Finalize:

"During shutdown of an application domain, Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible."

RichardOD
"If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose." -this is from msdn as well, doesn't it mean that the connection may not be closed if you don't call Close()?
agnieszka
You basically can do Close, Dispose or using() {}, as you should be doing anyway. When the app closes the connections will also close, for the reason I have stated in my answer.
RichardOD
Also if at any time the GC decides to finalise the connections, then they will also be closed.
RichardOD
+1  A: 

SQL connections are expensive to create and ADO.NET uses a technique called connection pooling which allows to reuse them.

Quote from MSDN:

It is strongly recommended that you always close the connection when you are finished using it so that it will be returned to the connection pool and be reused.

If the maximum pool size has been reached and no usable connection is available, the request is queued. The pooler then tries to reclaim any connections until the time-out is reached (the default is 15 seconds). If the pooler cannot satisfy the request before the connection times out, an exception is thrown.

Darin Dimitrov
In fact because of the connection pool, an SQL connection is never actually closed - it is just returned to the pool in order to be reused when you call Close. It will be close when the application domain is destroyed that is when you exit the application. So if you never call Close the connection won't be reused and once the pool is exhausted you will get an exception.
Darin Dimitrov
@darin- never actually closed is incorrect. After a certain amount of inactivity the connection will be closed, but that's just nitpicking.
RichardOD
@RichardOD, I wouldn't say it's nitpicking. I actually didn't know this fact. Thank you very much for this precision.
Darin Dimitrov
+1  A: 

When you exit the application in a regular fashion, I expect the finalizers to run. They will close the connection if it is still open. Just don't depend on this: it might take a while before those finalizers are run in normal operation of your application, so you will be keeping too many connections open.

When the application crashes, maybe the finalizer will not run, leaving the connection open past the lifetime of the application.

Hans Kesting
+2  A: 

Another thing to keep in mind here is that in .Net, you can wrap your connections in a using block, and that will close and dispose your connections for you. So the lack of an explicit Close() isn't a bad thing if you've got your using blocks there...

// this using block will auto close & dispose your connection...
using (var conn = new SqlConnection(...))
{
    conn.Open();
    // database code here with no explicit close

}

that is the functional equivalent of a try/finally block with a conn.close in the finally. Many devs overlook the using blocks - make sure you're not doing the same in this case.

If you do rewrite your code to close your connections - it's good practice to use Using blocks around all of your database objects (connection, command, reader) to make sure that they are closing and disposing when they fall out of scope of the using block. I'd definitely suggest writing those into your code instead of just conn.Close() where needed.

Scott Ivey
Yeap that's the way I do it. If you use FXCop you won't ever have an issue as it warns you about not calling Dispose.
RichardOD
There was no using block, i know about them.
agnieszka
and it's not the answer to the question
agnieszka
@agnieszka - you'd be surprised how many people overlook that and call the lack of a Close a bug when its in the using block. I was just pointing out what many people overlook. I was also researching the answer to the rest of your question when you downvoted me, so thanks a ton for that...
Scott Ivey