PC Review


Reply
Thread Tools Rate Thread

Closing database connections in a class destructor

 
 
Mark Rae
Guest
Posts: n/a
 
      8th Feb 2005
"Charlie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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...


 
Reply With Quote
 
 
 
 
Charlie
Guest
Posts: n/a
 
      8th Feb 2005
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


 
Reply With Quote
 
Chris, Master of All Things Insignificant
Guest
Posts: n/a
 
      8th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
=?Utf-8?B?VHUtVGhhY2g=?=
Guest
Posts: n/a
 
      8th Feb 2005
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.

"Charlie" wrote:

> 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
>
>
>

 
Reply With Quote
 
Ollie Riches
Guest
Posts: n/a
 
      8th Feb 2005
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

"Charlie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
RCS
Guest
Posts: n/a
 
      8th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      8th Feb 2005
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


 
Reply With Quote
 
Dan Shanyfelt MCAD
Guest
Posts: n/a
 
      8th Feb 2005
Is it better to go with the
finally {conn.dispose;}
or
using (con = new conn)
?

-Dan

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      10th Feb 2005
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


 
Reply With Quote
 
CSharper
Guest
Posts: n/a
 
      28th Apr 2005
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

"Dan Shanyfelt MCAD" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is it better to go with the
> finally {conn.dispose;}
> or
> using (con = new conn)
> ?
>
> -Dan
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A class destructor Trecius Microsoft C# .NET 4 20th Oct 2008 04:40 PM
C# not closing database connections(?) MarkusJNZ@gmail.com Microsoft C# .NET 5 30th Oct 2006 11:01 PM
Destructor for VB.NET class Mika M Microsoft VB .NET 2 4th Apr 2005 01:03 PM
Query about closing connections to SQL Server database. Oenone Microsoft ADO .NET 1 11th Nov 2004 05:10 PM
Closing out database connections. Timeouts Do Microsoft ASP .NET 2 9th Dec 2003 10:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:40 PM.