473,320 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Closing IDbConnection

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
3 3030
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pei_world | last post by:
I am create an generic class for Database access using these interfaces. I run my program with the idea of IDbConnection and IDbDataAdatper without any problem in single table. But since...
0
by: karunakar | last post by:
Hi all Iam trying to connect the SQL & sybase These dabases need to connect the Same Code I have littile bit idea of this But iam not able to populate the Datagrid Using "IDBConnection" and...
6
by: Al the programmer | last post by:
I want to catch the Closing event for my form. I created a test windows app using the wizard. I then create the Closing event by clicking the lightning bolt on the properties pane. The code is...
1
by: **Developer** | last post by:
When I get a closing event in a MID Child form I don't know if the child form is closing or the main form is closing. Is there a way to tell? Thank
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
0
by: Ryan Liu | last post by:
Hi, I downloaded the latest version of mysql 5.0 and its .NET driver, I copied the sample code from mysql 5.0 manual to do use prepared statement in C#. But indeed I got a run time error say...
4
by: Academic | last post by:
Does it make sense to put this If e.Cancel Then Exit Sub at the beginning of form closing events so if the user cancels the app's exiting in one Closing routine he will not be asked again by...
8
by: JoeW | last post by:
I normally use SqlConnection, SqlDataReader, SqlCommand for most of my queries but recently a friend of mine who is a long time java programmer suggested I use the IDbConnection, IDbCommand...
1
by: Paul Rubin | last post by:
it looks like contextlib.closing fails to be idempotent, i.e. wrapping closing() around another closing() doesn't work. This is annoying because the idea of closing() is to let you use legacy...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.