Closing database connections in a class destructor

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

I have a destructor in my base class that is supposed to close a database
connection when class is released.

~BaseTable()
{
this.Conn.Close();
}

However, connection is never closed. It doesn't appear destructor is
firing. Why?

1) After you've finished with the instance of the class, are you setting it
to null? If so, and nothing else is using it, the GC should collect it,
causing your destructor to fire...
 
Hi:

I have a destructor in my base class that is supposed to close a database
connection when class is released.

~BaseTable()
{
this.Conn.Close();
}

However, connection is never closed. It doesn't appear destructor is
firing. Why?

Thanks,
Charlie
 
Why do you think it isn't closed? AFAIK the destructor only runs when the
garbage collector does its thing. You won't know for sure when it gets run.
If you need to be sure it closes in a timely manner, call the Conn.Close
directly.

Chris
 
Charlie,

The runtime controls when it calls the destructor. You need to take that
into consideration when closing your resources. Maybe you can implement
IDisposable interface and close the connection there.
 
firstly you shouldn't have a destructor for a class unless the class is
implementing the IDisposable interface and secondly you should close a
database connection as soon as you have finished with it.

HTH

Ollie Riches
 
If at all possible, it's proper to use the "Using" statement to set the
scope of a database connection:

using ( SqlConnection Conn = new SqlConnection() )
{
// do your database work
}

As soon as you leave that curly brace, the scope of that variable goes away
and will "immediately" goto garbage collection..
 
Charlie,

Beside all answers you have had already is this one of the few places where
is advised to use the connection.dispose in dotnet 2002/2003. It seems that
there is done some extra overloading for the connection pooling not
mentioned on MSDN. On MSDN is the difference between the close and the
dispose that the reference to the connectionstring is removed.

Just a little addition.

Cor
 
Is it better to go with the
finally {conn.dispose;}
or
using (con = new conn)
?

-Dan
 
Dan,

I don't know the only thing I know is that Angel (MS AdoNet) is always
writting that they have used the connection.dispose to do something extra
for connection pooling in the versions 2002 and 2003 while they have a new
implementation for that in the version 2005.

How far that is affected with the real dispose, I don't know as I said
before.

Cor
 
Basically the same thing. Either way is still better than setting it to
null, since the SqlConnection's Dispose method explicitly closes the
connection.

Dan Cox
 
They're basically the same thing. Either way is still better than simply
setting it to null, since the SqlConnection's Dispose method explicitly
closes the connection.

Dan Cox
 
They're basically the same thing. Either way is still better than simply
setting it to null, since the SqlConnection's Dispose method explicitly
closes the connection.

Dan Cox
 
They're basically the same thing. Either way is still better than simply
setting it to null, since the SqlConnection's Dispose method explicitly
closes the connection.

Dan Cox
 
They're basically the same thing. Either way is still better than simply
setting it to null, since the SqlConnection's Dispose method explicitly
closes the connection.

Dan Cox
 
I normally do not use "using".

The issue is that I normally have more than one thing to close.

Multiple "using" statements make the sourec look awfull, indentaion wise.

Multiple closingsin the finally do not.

--
Regards

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
(CTO PowerNodes Ltd.)
 
Back
Top