I read this question on relating to disposing of SQL connections.
My question is, how bad is it to simply close a sql connection, but not dispose of it? We have a function which is simply closed, but never disposed, and it is used 1000s times a day. Is it better to simply close it, or would it be better to close and dispose of it?
I am aware that dispose() also closes the connection, however I would like to know why close doesn't dispose of the connection.
The important thing about connections is to close them so they are returned to the connection pool.
As such, there is little difference between disposing and closing a connection, so long as you are disciplined about closing and not reusing connections.
However, being in the habit of wrapping the creation of a connection in a using
statement means you never forget to close it.
It is a general good idiom to follow - creation of any object that implements IDisposable
should be wrapped in a using
statement, and as such an idiom it is a good one to follow with connections as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With