473,566 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it necessary to dispose a System.Data.IDb Connection object???

Hi All,
A small confusion. I have defined a connection class that has
System.Data.IDb Connection 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.SuppressFina lize(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 6038
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)difference s 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.ID bConnection as a member variable and implements IDisposable
interface. I have implemented Dispose method to call Dispose method of
IDbConnectio n. This Dispose method is called from a destructor. In the
dispose method, i also call GC.SuppressFina lize(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.SupressFinal ize is
self explanatory.
4. Check the code below:
public void Dispose()
{
if(mIConnection != null)
{
mIConnection.Di spose();
mIConnection = null;
}

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

GC.SuppressFina lize(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)difference s 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.ID bConnection as a member variable and implements IDisposable
interface. I have implemented Dispose method to call Dispose method of
IDbConnectio n. This Dispose method is called from a destructor. In the
dispose method, i also call GC.SuppressFina lize(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 deterministical ly 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.Di spose() is equivalent to mIConnection.Cl ose(), 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.SupressFinal ize is
self explanatory.
4. Check the code below:
public void Dispose()
{
if(mIConnection != null)
{
mIConnection.Di spose();
mIConnection = null;
}

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

GC.SuppressFina lize(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)difference s 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.ID bConnection as a member variable and implements IDisposable
>interface. I have implemented Dispose method to call Dispose method of
>IDbConnectio n. This Dispose method is called from a destructor. In the
>dispose method, i also call GC.SuppressFina lize(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
11110
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 As EventArgs) DataGrid1.DataSource = MyQueryMethod(CInt(TextBox1.Text)) DataGrid1.DataBind() End Sub Function MyQueryMethod(ByVal others As...
11
2552
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 dispose itself when not in use? I know I can use the clear method, but that just clear the data within dataset not dispose of the dataset all...
5
9635
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 this error? Oracle error occurred, but error message could not be retrieved from Oracle. Description: An unhandled exception occurred during the...
9
20130
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 control. When I close the form I am expecting that the control ceases to be. However, if my object raises the event after the form has been closed, I...
156
5779
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 inside a method call. dim myvar as new object1 object1.dosomethingmethod(new object2) Note that object 2 has a dispose method but how do I...
3
330
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 Dispose method of IDbConnection. This Dispose method is called from a destructor. In the dispose method, i also call GC.SuppressFinalize(this) so...
1
1816
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 ConnectionPool? Do they have the same effection source code? If they are different, who can tell me the differences? If they are same, why MS gives the...
1
1898
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 complete. I have error handling in my code but not on the dispose method. I call the dispose method exiting the application using the code below but...
44
8198
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 End Class now where would I properly dispose of this? In the finalize method? I am loading the data for the table in a subroutine that is...
0
7673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8109
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.