Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Database Connection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Trecius - 21 Aug 2007 19:14 GMT
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 with a solution -- I believe -- should work, but
doesn't work.  I'd like to ask for your continued feedback.

I create a class that wraps the DBConnection.  Here's a small sample...

class CDB : IDispose
{
 ...
 protected System.Data.Common.DBConnection m_conn = null;

 public CDB()
 {
   ...
   this.m_conn.Open();
   System.GC.SuppressFinalize(this.m_conn);  // IMPORTANT
 }
 public Disconnect()
 {
   if (this.m_conn.State == System.Data.ConnectionState.Open)
   {
     this.m_conn.Close();
   }
 }
 ...
}

Now for my Dispose() and destructor, which is a Finalize() method, I have
the following...

~CDB()
{
 this.Disconnect();
 this.m_conn.Dispose();
}

public void Dispose()
{
 this.Disconnect();
 this.m_conn.Dispose();
 System.GC.SuppressFinalize(this);
}

Now, when I create an instance of my wrapper, it can connect and perform
queries to the specified database.  However, when the object goes out of
scope, it calls the destructor.  In the destructor, it calls Disconnect(),
where I get an error on the line that is "this.m_conn.Close()" stating the
handle is not initialized, but I told the garbage collector to NOT call
Finalize() on the this.m_conn.  I told it not to do this in the constructor
of my wrapped object.  So why is the m_conn being GC when I told it not to?

Thank you all for your continued support and patience.

Trecius
Jon Skeet [C# MVP] - 21 Aug 2007 19:23 GMT
> 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 with a solution -- I believe -- should work, but
> doesn't work.  I'd like to ask for your continued feedback.

One reason it shouldn't work is your claim here:

> Now, when I create an instance of my wrapper, it can connect and perform
> queries to the specified database.  However, when the object goes out of
> scope, it calls the destructor.

An object doesn't go out of scope: a variable does. That, in itself,
won't cause the finalizer to be called. The finalizer will be called
*some point* after the object is no longer referenced.

As to the rest of your post, I'd be interested in seeing a short but
complete example demonstrating the problem. I can't say I've ever seen
SuppressFinalize being called on anything other than "this" - it seems
like a bad idea to me.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Peter Bromberg [C# MVP] - 22 Aug 2007 01:34 GMT
This code is "overkill". All you need to do is make a call to Close or
Dispose on your connection instance as soon as you are finished using it, and
allow it to go back to the ADO.NET connection pool. Forget about all the
finalizer / destructor stuff.
Peter

Signature

Recursion: see Recursion
site:  http://www.eggheadcafe.com
unBlog:  http://petesbloggerama.blogspot.com
BlogMetaFinder:    http://www.blogmetafinder.com

> Hello, Newsgroupians:
>
[quoted text clipped - 53 lines]
>
> Trecius
Alvin Bruney [MVP] - 23 Aug 2007 01:03 GMT
Why not use the MS DAAB? It already works out all that stuff properly.

Signature

Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc

> This code is "overkill". All you need to do is make a call to Close or
> Dispose on your connection instance as soon as you are finished using it,
[quoted text clipped - 65 lines]
>>
>> Trecius

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.