By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

Closing IDbConnection

P: n/a
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
Sep 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Peter Kirk" wrote:
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
Sep 22 '06 #2

P: n/a
On Fri, 22 Sep 2006 10:12:17 +0200, Peter Kirk wrote:
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.
--
Tom Porterfield
Sep 22 '06 #3

P: n/a
"Peter Kirk" <pk@alpha-solutions.dkskrev i en meddelelse
news:uk**************@TK2MSFTNGP04.phx.gbl...
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.
Sep 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.