By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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...f-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.

Jul 21 '05 #2

P: n/a
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]"" wrote:
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...f-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.


Jul 21 '05 #3

P: n/a

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/de...zedispose.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...21/232445.aspx
http://weblogs.asp.net/clyon/archive...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]"" wrote:
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...f-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.
>


Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.