Closing IDbConnection

P

Peter Kirk

Hi

do I need to explicitly close IDbConnection and IDbCommand objects? Or are
they disposed of when the method they are used in ends?

Normally I use a construct like:

using (IDbConnection conn = GetConnection()) // where "GetConnection gets a
database connection
{
// do database stuff
}

and as I understand it this ensures that the connection object is correctly
closed (even if an exception is thrown when the connection object is used).

But I have also seen code without any form of "using" or
"try/catch/finally". Will the obtained connection in these cases be
correctly closed when the method using the connection ends?

thanks,
Peter
 
G

Guest

It is best to use the using(){} statement to ensure the connection is closed
and releases its resources. I dont think the connection is return to the pool
just for going out of scope. I think they are released when the are garbage
collected, which depending on you app might not be to soon after they are
used.

HTH

Ciaran O'Donnell
 
T

Tom Porterfield

Hi

do I need to explicitly close IDbConnection and IDbCommand objects? Or are
they disposed of when the method they are used in ends?

Normally I use a construct like:

using (IDbConnection conn = GetConnection()) // where "GetConnection gets a
database connection
{
// do database stuff
}

and as I understand it this ensures that the connection object is correctly
closed (even if an exception is thrown when the connection object is used).

But I have also seen code without any form of "using" or
"try/catch/finally". Will the obtained connection in these cases be
correctly closed when the method using the connection ends?

The Dispose method on the connection closes the connection, so putting its
instance in a using block as your example will insure that it is closed.
If the connection instance is not in a using block as above, and the
connection is not explicitly closed or disposed, then it will remain open
until garbage collected. This is not a good practice as it means that
resource is unavailable to others until such time as the GC does its work.
 
P

Peter Kirk

do I need to explicitly close IDbConnection and IDbCommand objects? Or are
they disposed of when the method they are used in ends?

Normally I use a construct like:

using (IDbConnection conn = GetConnection()) // where "GetConnection gets
a database connection
{
// do database stuff
}

and as I understand it this ensures that the connection object is
correctly closed (even if an exception is thrown when the connection
object is used).

But I have also seen code without any form of "using" or
"try/catch/finally". Will the obtained connection in these cases be
correctly closed when the method using the connection ends?

Thanks for the answers. I had guessed correctly what was happening, but just
wanted confimation. Now I have been through the code and ensured that all
connections are disposed correctly (with using constructs). Now the issues
we were having with the application slowing down markedly every so often
have disappeared - most like caused by the database running out of
connections and the garbage collector running to close them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top