Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing(), but not disposing() SQL Connection - effects

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.

like image 861
Peter PitLock Avatar asked Oct 06 '22 08:10

Peter PitLock


1 Answers

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.

like image 183
Oded Avatar answered Oct 20 '22 00:10

Oded