473,386 Members | 1,715 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,386 software developers and data experts.

Closing database connections in a class destructor

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
Nov 16 '05 #1
15 9088
"Charlie" <cf*******@radiustravel.com> wrote in message
news:e1*************@TK2MSFTNGP09.phx.gbl...
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...
Nov 16 '05 #2
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" <cf*******@radiustravel.com> wrote in message
news:e1*************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #3
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

Nov 16 '05 #4
RCS
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" <cf*******@radiustravel.com> wrote in message
news:e1*************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #5
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" <cf*******@radiustravel.com> wrote in message
news:e1*************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #6
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
Nov 16 '05 #7
Is it better to go with the
finally {conn.dispose;}
or
using (con = new conn)
?

-Dan

Nov 16 '05 #8
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
Nov 16 '05 #9
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" <da*************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Is it better to go with the
finally {conn.dispose;}
or
using (con = new conn)
?

-Dan

Nov 17 '05 #10
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

"Dan Shanyfelt MCAD" <da*************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Is it better to go with the
finally {conn.dispose;}
or
using (con = new conn)
?

-Dan

Nov 17 '05 #11
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

"Dan Shanyfelt MCAD" <da*************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Is it better to go with the
finally {conn.dispose;}
or
using (con = new conn)
?

-Dan

Nov 17 '05 #12
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

"Dan Shanyfelt MCAD" <da*************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Is it better to go with the
finally {conn.dispose;}
or
using (con = new conn)
?

-Dan

Nov 17 '05 #13
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

"Dan Shanyfelt MCAD" <da*************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Is it better to go with the
finally {conn.dispose;}
or
using (con = new conn)
?

-Dan

Nov 17 '05 #14
Sorry for the multiple reposts...darn newsgroup server.
"CSharper" <do**@botherme.com> wrote in message
news:d7********************@bignews4.bellsouth.net ...
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

"Dan Shanyfelt MCAD" <da*************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Is it better to go with the
finally {conn.dispose;}
or
using (con = new conn)
?

-Dan


Nov 17 '05 #15
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.)
---

Still waiting for ObjectSpaces? Try the EntityBroker today - more versatile,
more powerfull.
And something in use NOW. for the projects you have to deliver - NOW.
"CSharper" <do**@botherme.com> wrote in message
news:d7********************@bignews4.bellsouth.net ...
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

"Dan Shanyfelt MCAD" <da*************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Is it better to go with the
finally {conn.dispose;}
or
using (con = new conn)
?

-Dan


Nov 17 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dustin lee | last post by:
Over the years I've gotten out of the habit of explicitly closing file objects (whether for reading or writing) since the right thing always seems to happen auto-magically (e.g. files get written...
6
by: MAB71 | last post by:
There should be a way to copy an access database ( .mdb file ) without closing connections to it. Unfortunately the FileCopy statement in VB gives an error if users are connected to the db. But I...
1
by: csharpbeginner | last post by:
My requirement is a certain database connection should be closed only when the object is unloaded from memory. Is the class destructor a good place to close open database connections? Thanks in...
1
by: C Sharp beginner | last post by:
I'm sorry about this verbose posting. This is a follow-up to my yesterday's posting. Thanks William for your reply. I understand it is a good practice to open connections as late as possible and...
2
by: juli | last post by:
Hello ppl! How exactly do I close all the database connections that was open in the config file and how do I use the destructor in C#. Thanks a lot!
7
by: darrel | last post by:
We're running into a problem on our new site. Once a week or so, our site goes down with an 'out of memory error'. Rebooting the web server fixes things. Googling the error doesn't return many...
5
by: Daves | last post by:
is it a good or bad practise to use the session_start & session_end for opening & closing the database connection? I'd thought that on powerful servers today the time the connection is kept locked...
3
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: Recently I asked a question regarding database connections and how to properly close the connection upon the object going out of scope. After much research, I've come up...
1
by: Danigan | last post by:
I've often struggled with database calls--not from a coding standpoint, but from the standpoint of how to make them more self-maintaining. My dream would be this: An include file which contains...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.