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