Is it necessary to dispose a System.Data.IDbConnection object???

G

Guest

Hi All,
A small confusion. I have defined a connection class that has
System.Data.IDbConnection as a member variable and implements IDisposable
interface. I have implemented Dispose method to call Dispose method of
IDbConnection. This Dispose method is called from a destructor. In the
dispose method, i also call GC.SuppressFinalize(this) so as to avoid
finalizer. All this was OK untill i came across one article that says - not
to call dispose in finalize on connection/transaction/datareader objects as
these are managed resourses as this eats up system resources. Also is it
really required to implement IDisposable interface for this connection
object???? Simply closing the connection and setting it to null is sufficient
enough (I feel so but need concrete answer and also i consider all .NET
classes as managed resources if im not wrong)???? Also if there is no need to
call Dispose on connection then why the hell microsoft has exposed this
dispose method???? Please help me!!!!
Thanks in advance.
 
C

Chris Lyon [MSFT]

Calling Dispose() should be equivalent to calling Close. Microsoft made it available so you could declare and the connection object inside a C# using block.

Here's a blog that has a good explanation of the (non)differences between Close and Dispose:
http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=1fe0f820-5b2b-4b17-82af-08142e7f308a

And yes, you should not call Dispose from a finalizer. You should call Dispose as soon as you can after being finished with the object.

Hope that helps
-Chris
 
G

Guest

Hi Chris,
Thanks for the link. Just to be sure that what i understand i right and plz
correct me if im wrong. Plz read carefully.......
1. Close and Dispose do the same operation. Dispose calls Close internally.
No need to call Close when Dispose is called though they do not throw any
exception if both are called .
2. The only reason you need to implement IDisposable interface is to release
unmanaged resources and in this case (Connection object) is not required.
(Check Point 3). You can simply close the connection object.
3. But there is a possibility to forget to call Close/Dispose method so
there is a need to implement the rather deterministic destructor of your
class to Close/Dispose the connection object. Secondly to keep consistancy
with the .NET Framework classes, it would be good(but not a requirement) to
override Dispose method of base class to provide custom Dispose method. Now
since both the destructor as well as the Dispose method do the same
operation, there is a need of a flag. Also the need of GC.SupressFinalize is
self explanatory.
4. Check the code below:
public void Dispose()
{
if(mIConnection != null)
{
mIConnection.Dispose();
mIConnection = null;
}

GC.SuppressFinalize(this);
}
Also can be done as
public void Dispose()
{
if(mIConnection != null)
{
mIConnection.Close(); //Changed code
mIConnection = null;
}

GC.SuppressFinalize(this);
}
If all the above points are correct than hip-hip-hurray.....
Thanks in advance.


"Chris Lyon [MSFT]" said:
Calling Dispose() should be equivalent to calling Close. Microsoft made it available so you could declare and the connection object inside a C# using block.

Here's a blog that has a good explanation of the (non)differences between Close and Dispose:
http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=1fe0f820-5b2b-4b17-82af-08142e7f308a

And yes, you should not call Dispose from a finalizer. You should call Dispose as soon as you can after being finished with the object.

Hope that helps
-Chris

--------------------
Hi All,
A small confusion. I have defined a connection class that has
System.Data.IDbConnection as a member variable and implements IDisposable
interface. I have implemented Dispose method to call Dispose method of
IDbConnection. This Dispose method is called from a destructor. In the
dispose method, i also call GC.SuppressFinalize(this) so as to avoid
finalizer. All this was OK untill i came across one article that says - not
to call dispose in finalize on connection/transaction/datareader objects as
these are managed resourses as this eats up system resources. Also is it
really required to implement IDisposable interface for this connection
object???? Simply closing the connection and setting it to null is sufficient
enough (I feel so but need concrete answer and also i consider all .NET
classes as managed resources if im not wrong)???? Also if there is no need to
call Dispose on connection then why the hell microsoft has exposed this
dispose method???? Please help me!!!!
Thanks in advance.
 
C

Chris Lyon [MSFT]

1. This is correct, with the caveat that you're working on the assumption that the class author correctly implemented the Dispose Pattern
(http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconfinalizedispose.asp).

2. IDisposable is best suited for unmanaged resources, but not exclusively. If you are using unmanaged resources, IDisposable should be implemented, but not all classes
that implement IDisposable hold unmanaged resources. Personally, I call Dispose on all objects that implement IDisposable as soon as I'm done with them.

3. Yes, users may forget to call Dispose/Close, so there should be cleanup in your finalizer. However, the finalizer is not deterministically run, nor is it even guaranteed to run. If
you require a finalizer to ensure cleanup of unamanaged resources, correctly following the Dispose Pattern is highly recommended.

4. If you can be sure that mIConnection.Dispose() is equivalent to mIConnection.Close(), then yes, they are interchangeable (you can assume this for classes in the .NET
Framework with both Close and Dispose methods, check with 3rd party vendors, etc).

I have a couple blog entries about Dispose that may be helpful:
http://weblogs.asp.net/clyon/archive/2004/09/21/232445.aspx
http://weblogs.asp.net/clyon/archive/2004/09/23/233464.aspx

-Chris

--------------------
Hi Chris,
Thanks for the link. Just to be sure that what i understand i right and plz
correct me if im wrong. Plz read carefully.......
1. Close and Dispose do the same operation. Dispose calls Close internally.
No need to call Close when Dispose is called though they do not throw any
exception if both are called .
2. The only reason you need to implement IDisposable interface is to release
unmanaged resources and in this case (Connection object) is not required.
(Check Point 3). You can simply close the connection object.
3. But there is a possibility to forget to call Close/Dispose method so
there is a need to implement the rather deterministic destructor of your
class to Close/Dispose the connection object. Secondly to keep consistancy
with the .NET Framework classes, it would be good(but not a requirement) to
override Dispose method of base class to provide custom Dispose method. Now
since both the destructor as well as the Dispose method do the same
operation, there is a need of a flag. Also the need of GC.SupressFinalize is
self explanatory.
4. Check the code below:
public void Dispose()
{
if(mIConnection != null)
{
mIConnection.Dispose();
mIConnection = null;
}

GC.SuppressFinalize(this);
}
Also can be done as
public void Dispose()
{
if(mIConnection != null)
{
mIConnection.Close(); //Changed code
mIConnection = null;
}

GC.SuppressFinalize(this);
}
If all the above points are correct than hip-hip-hurray.....
Thanks in advance.


"Chris Lyon [MSFT]" said:
Calling Dispose() should be equivalent to calling Close. Microsoft made it available so you could declare and the connection object inside a C# using block.

Here's a blog that has a good explanation of the (non)differences between Close and Dispose:
http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=1fe0f820-5b2b-4b17-82af-08142e7f308a

And yes, you should not call Dispose from a finalizer. You should call Dispose as soon as you can after being finished with the object.

Hope that helps
-Chris

--------------------
Hi All,
A small confusion. I have defined a connection class that has
System.Data.IDbConnection as a member variable and implements IDisposable
interface. I have implemented Dispose method to call Dispose method of
IDbConnection. This Dispose method is called from a destructor. In the
dispose method, i also call GC.SuppressFinalize(this) so as to avoid
finalizer. All this was OK untill i came across one article that says - not
to call dispose in finalize on connection/transaction/datareader objects as
these are managed resourses as this eats up system resources. Also is it
really required to implement IDisposable interface for this connection
object???? Simply closing the connection and setting it to null is sufficient
enough (I feel so but need concrete answer and also i consider all .NET
classes as managed resources if im not wrong)???? Also if there is no need to
call Dispose on connection then why the hell microsoft has exposed this
dispose method???? Please help me!!!!
Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

.net object dispose 4
Dispose, 1
object dispose 2
Dispose Overkill? 7
.net object dispose question 1
Is an inherited base object called an object's parent? 1
Dispose variables to free memory 1
Dispose pattern 25

Top