473,378 Members | 1,387 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,378 software developers and data experts.

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 4750
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._objectState)
{
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**************@TK2MSFTNGP12.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.Dispose(),Dispose method itself closes the
connection and make the reffrence of the connection object to null. Its
fine if you just use the Connection.Dispose().
-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**************@TK2MSFTNGP09.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*********@poole.gov.uk> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.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
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...
7
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...
10
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...
6
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...
3
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?) ...
35
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...
6
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...
3
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....
3
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.