473,756 Members | 3,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conn.Close & Conn.Dispose

Hi All,

Do I need to use both Conn.Close() & Conn.Dispose() when I have finished
with an SQL connection?
--
I am using the free version of SPAMfighter for private users.
It has removed 2425 spam emails to date.
Paying users do not have this message in their emails.
Try www.SPAMfighter.com for free now!
Nov 19 '05 #1
6 4761
This can lead to heated debate, hopefully I'll answer first and then you can
ignore everyone else ;)

Seriously though, this is what Dispose does (actual code):
switch (this._objectSt ate)
{
case ConnectionState .Open:
{
this.Close();
break;
}
}
this._constr = null;
as you can see, all it does it call Close(). So you might be tempted to say
"i should just call close and save some stack". However, there is a dipose
pattern which you should follow. while you can call one, or the other or
both to achieve the same result, this behaviour could change with future
release. One day (for example 2.0) Dispose might do more...and then you'd
have to go through all your code and make sure you had called Dispose.
Additionally, languages like C# are dispose-aware thanks to the using
keyword.

As far as i'm concerned, those two points are enough to make sure I always
call Dipose on classes which inherit IDisposable.

As for close, my personal feeling is that it doesn't need to be called if
you are calling Dispose. And, since it's better to be consistent, I'd say
never call it. Why? well, none of the .net language are close-aware, and by
definition of what Dispose does, it would be a mistake from the class
designer (ie, microsoft) to implement cleanup functionality in close which
Dispose also woudlnt' do (possibly simply by running close, as it currently
does).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Simon Harris" <to***********@ makes-you-fat.com> wrote in message
news:ec******** ******@TK2MSFTN GP12.phx.gbl...
Hi All,

Do I need to use both Conn.Close() & Conn.Dispose() when I have finished
with an SQL connection?
--
I am using the free version of SPAMfighter for private users.
It has removed 2425 spam emails to date.
Paying users do not have this message in their emails.
Try www.SPAMfighter.com for free now!

Nov 19 '05 #2
If You use Connection.Disp ose(),Dispose method itself closes the
connection and make the reffrence of the connection object to null. Its
fine if you just use the Connection.Disp ose().
-Ram

Nov 19 '05 #3
Thank you both for your answers. I think I'll just use dispose from now on.

Whilst were on the subject, we had a situation recently where an application
was not releasing connections back into the connection pool, even though we
were calling both .close and .dispose. I'm afraid I dont have any more
details to hand, but I do know one of our developers spent a week on the
problem, eventually giving in and turning off connection pooling on the
server.

Has anyone come across this?

Thanks again,
Simon.
Nov 19 '05 #4
If you had integrated security (windows login) and windows authentication
on, I'd expect it to happen. Else I wouldn't know without further detail.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Simon Harris" <to***********@ makes-you-fat.com> wrote in message
news:eZ******** ******@TK2MSFTN GP09.phx.gbl...
Thank you both for your answers. I think I'll just use dispose from now
on.

Whilst were on the subject, we had a situation recently where an
application was not releasing connections back into the connection pool,
even though we were calling both .close and .dispose. I'm afraid I dont
have any more details to hand, but I do know one of our developers spent a
week on the problem, eventually giving in and turning off connection
pooling on the server.

Has anyone come across this?

Thanks again,
Simon.

Nov 19 '05 #5
Hi,

Yes, we are using integrated security (It's an Intranet). How is this
related?

Thanks.
Karl Seguin wrote:
If you had integrated security (windows login) and windows authentication
on, I'd expect it to happen. Else I wouldn't know without further detail.

Karl


Nov 19 '05 #6
Check out:
http://weblogs.asp.net/sjoseph/archi...23/395601.aspx

and do a search for "integrated security" ...connections are pooled based on
their connnection string, and w/integrated security the connection string is
different per user, hence no pooling happens cross-user. My knowledge of
this is very limited, just what I've read in passing...not sure if it's the
problem you saw, but it's the only thing I know..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Web Team @ Borough of Poole" <we*********@po ole.gov.uk> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Hi,

Yes, we are using integrated security (It's an Intranet). How is this
related?

Thanks.
Karl Seguin wrote:
If you had integrated security (windows login) and windows authentication
on, I'd expect it to happen. Else I wouldn't know without further detail.

Karl

Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2131
by: Tim Bücker | last post by:
I´ve got a little program with the "x" in the upper right corner and a menu item "close" to do the same - end the program. The problem now is that using my program it gets sometimes impossible to end the program. Clicking the "x" nothing happens and calling this.Close(); when the menu item "close" is pressed also doesn´t change anything. But calling this.Close(); this.Dispose();
7
3015
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the "Close()" methods reared their ugly (at least it would seem...)heads. I was happily delving away in the .NET framework, investigating the stream classes with the msdn and Lutz Roeder's .NET reflector, when I stumbled upon the following:
10
6284
by: Jim H | last post by:
I sometimes get the following error from my Form's Dispose Method when the application is closing: ------------------------------------------------- An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll Additional information: Cannot call Dispose() while doing CreateHandle(). ------------------------------------------------- How do I find out what is calling CreateHandle()? Here is the...
6
1797
by: Ashish | last post by:
It might be basics for many but I never gave attention on this before. Steps: Add 2 forms (Form1/Form2) in application. Create a object in Form1 for Form2. Form2 f2 = new Form2(); //line 1 //f2.Show(); // line 2 f2.Close(); // line 3
3
4275
by: FC | last post by:
Hello All: I am getting the following error: Compiler Error Message: CS0246: The type or namespace name 'conn' could not be found (are you missing a using directive or an assembly reference?) This error point to the line conn.Close() on my finally statement. I am pretty new at this and I did not find much on the web. Any help would be greatly appreciated. My code is as follows: <%@ Page Language="C#" debug="true" %>
35
11420
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing Then CType(cn, IDisposable).Dispose() End If I have to admit, I'm not sure what happens here. Will someone explain this line of code (the middle one, not the if statement LOL) to me please?
6
1637
by: Nate | last post by:
I am in a slight predicament trying to determine the most efficient and effective way to connect/disconnect from a database within a business object (c# dll). I'm also keeping in mind the concept of connecting late and disconnecting early. Background: - multi-tier application (code-behind uses properties and methods of the business object, the business object handles the data layer) For instance (in an ASPX code-behind file):
3
5944
by: Karan | last post by:
I am calling finalize when form2 loads and deactivates form1 which closes form1. However, same thing is not happening in form2 because finalize is already called. Does anybody has solution to it. This code works well for splash screen. I searched alot on net for codes but they don't work. for example- (1) Public Sub CloseForm(formType As System.Type) For Each oForm as System.Windows.Forms.Form in me.MdiParent.MdiChildren If...
3
9844
by: Joris De Groote | last post by:
Hi, I use Adobe Acrobat to read tekst from PDF files. After that the file has been read, I move the file in a folder (using the date I got from the text I got from Acrobat). Now here is my problem. When I want to move the file, I get an error stating: System.IO.IOException: The process cannot access the file "x:\VF\2006-01\CVF-06000007.pdf" because it is being used by another process.
0
9456
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10032
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9711
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8712
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5141
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5303
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.