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 3 1784
Trecius <Tr*****@discussions.microsoft.comwrote:
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.
--
Jon Skeet - <sk***@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
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
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com
"Trecius" wrote:
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
Why not use the MS DAAB? It already works out all that stuff properly.
--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc
"Peter Bromberg [C# MVP]" <pb*******@yahoo.yohohhoandabottleofrum.comwrote
in message news:F2**********************************@microsof t.com...
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
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com
"Trecius" wrote:
>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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mudge |
last post by:
Hi,
My hosting provider only allows me to use 50 connections to my MySQL
database that my Web site will use.
I don't know what this 50 connections means exactly. Does this mean that
only 50...
|
by: pradeep_TP |
last post by:
Hi all,
I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.
1) If I have opened a connection to a...
|
by: R Reyes |
last post by:
Hi, I'm trying to modularize my database connections a little better and get
more out of my project with less code.
First check out this common dbOpen() function inside class clsDatabase. I...
|
by: Nick Gilbert |
last post by:
Hi,
I have an asp.net application which runs from a CD-ROM using Cassini. As
such, it is single user only.
The application connects to an Access database when it is loaded, and
keeps the same...
|
by: Lau Lei Cheong |
last post by:
Hello,
Actually I think I should have had asked it long before, but somehow
I haven't.
Here's the scenerio: Say we have a few pages in an ASP.NET project,
each of them needs to connect to...
|
by: Martin B |
last post by:
Hallo!
I'm working with C# .NET 2.0, implementing Client/Server Applications which
are connecting via Network to SQL-Server or Oracle Databases.
To stay independent from the underlaying Database...
|
by: Terry Jolly |
last post by:
Web Solution
Goal: Have a global database connection
Why: (There will be 30+ tables, represented by 30+ classes) I only want to
reference the database connection once.
I put the connection...
|
by: Arsalan Ahmad |
last post by:
Hi all,
I am creating a website in which in an Item detail page there are a number
of web controls (7 or 8) and what is happening that inside each of control's
Page_Load() function I am creating...
|
by: Frinavale |
last post by:
How To Use A Database In Your Program
Many .NET solutions are database driven and so many of us often wonder how to access the database. To help you understand the answer to this question I've...
|
by: Curtis Rutland |
last post by:
How To Use A Database In Your Program Part II
This article is intended to extend Frinny’s excellent article: How to Use a Database in Your Program. Frinny’s article defines the basic concepts...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
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...
|
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...
|
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...
| |