473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
3 5998
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sbox | last post by:
I've got an error "System.FormatException: Input string was not in a correct format." while I'm implementing a datagrid and a textbox What's wrong with it? Sub Button1_Click(sender As Object, e...
11
by: Leon | last post by:
Are dataset automatically stored in memory? Does the dispose() method automatically dispose of the dataset in the code below? Do I have to dispose a dataset from memory or does the dataset...
5
by: petro | last post by:
Hello all, My asp.net web application works on my machine but I get the following error on our test web server, There is only one oracle home on the test server. Does anyone know how to resolve...
9
by: Charles Law | last post by:
I have a form on which user controls are placed at runtime. When a control is added to the form a handler is added for an event that a high-level object raises, which must be handled by the new...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
3
by: faktujaa | last post by:
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...
1
by: jinfeng_Wang | last post by:
hi, I have a question about the difference between SqlConnection.IDisposable.Dispose() and SqlConnection.Dispose(). Both of them realize the function of releasing the connection to the...
1
by: kuhrty | last post by:
Hi, I am creating a multi-winform project. Everything has been working fine, except after I perform an update to the database and decide to Exit the winform after the update operation is...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.