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

freeing database connections and other critical resources

Hi,
What is the best method to close database connections? Is
it enough if we just use the Close() method. Does the
connection get released immediately? or do we have to do
anything in the Dispose and Finalize methods? Your replies
are appreciated. Thanks in advance.
Nov 15 '05 #1
3 1313
Close is enough, that will release the connection back into the pool.

You do not need to call dispose.

<an*******@discussions.microsoft.com> wrote in message
news:70****************************@phx.gbl...
Hi,
What is the best method to close database connections? Is
it enough if we just use the Close() method. Does the
connection get released immediately? or do we have to do
anything in the Dispose and Finalize methods? Your replies
are appreciated. Thanks in advance.

Nov 15 '05 #2
The best method to close a database connection, by far, is with the 'using'
keyword:

using (SqlConnection conn = new SqlConnection(...))
{
// use connection here
}

This guarantees that Dispose() will be called for the connection when the
control exits the scope of the using statement. Most importantly, the call
to Dispose is guaranteed no matter how you exit the scope - even if there is
an exception.

SqlConnection.Dispose() calls SqlConnection.Close() which puts the physical
SQL connection back to the connection pool so it can be reused later.

Other classes that wrap critical (unmanaged) resources usually implement
IDisposable and they should also be used inside a 'using' statement whenever
possible.

Sami
www.capehill.net

<an*******@discussions.microsoft.com> wrote in message
news:70****************************@phx.gbl...
Hi,
What is the best method to close database connections? Is
it enough if we just use the Close() method. Does the
connection get released immediately? or do we have to do
anything in the Dispose and Finalize methods? Your replies
are appreciated. Thanks in advance.

Nov 15 '05 #3
Yes agreed with Sami on all he said here.

--
--------------------------------------------------------------
Sam Gentile [C#/.NET MVP]
..NET Blog http://samgentile.com/blog/
MSDN Column:
http://msdn.microsoft.com/library/de...tml/bridge.asp
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
---------------------------------------------------------------
"Sami Vaaraniemi" <sa***************@jippii.fi> wrote in message
news:c2**********@phys-news1.kolumbus.fi...
The best method to close a database connection, by far, is with the 'using' keyword:

using (SqlConnection conn = new SqlConnection(...))
{
// use connection here
}

This guarantees that Dispose() will be called for the connection when the
control exits the scope of the using statement. Most importantly, the call
to Dispose is guaranteed no matter how you exit the scope - even if there is an exception.

SqlConnection.Dispose() calls SqlConnection.Close() which puts the physical SQL connection back to the connection pool so it can be reused later.

Other classes that wrap critical (unmanaged) resources usually implement
IDisposable and they should also be used inside a 'using' statement whenever possible.

Sami
www.capehill.net

<an*******@discussions.microsoft.com> wrote in message
news:70****************************@phx.gbl...
Hi,
What is the best method to close database connections? Is
it enough if we just use the Close() method. Does the
connection get released immediately? or do we have to do
anything in the Dispose and Finalize methods? Your replies
are appreciated. Thanks in advance.


Nov 15 '05 #4

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

Similar topics

4
by: dustin lee | last post by:
Over the years I've gotten out of the habit of explicitly closing file objects (whether for reading or writing) since the right thing always seems to happen auto-magically (e.g. files get written...
5
by: Alex | last post by:
Hi all, We're looking at a vendor who uses the InterSystems Cache Database Platform, but our IT department has zero experience with this system. This software package will have a pivotal and...
14
by: L Mehl | last post by:
I tested a FE/BE application developed in A2000 on a A2002 machine and got this message when exiting the app. Clicking the only available button "OK", exits the application, as intended. The FE...
5
by: Ivan Weiss | last post by:
I declared a class called database. I have the following code: Dim myDatabase as Database myDatabase = Nothing Does the nothing keyword free the object for garbage collection or do I...
12
by: Joe Abou Jaoude | last post by:
hi, I have a component that uses a database connection. In the finalizer I dispose the connection because I read in msdn the following: "A type must implement Finalize when it uses...
17
by: Flic | last post by:
Is this possible? I know about a bit about ODBC and found how to import an ODBC database stored on the computer, however I am after accessing an SQL database stored on a webserver. I'd like to...
4
by: Sierra | last post by:
Problem: Database connections are not being reused properly. SP_WHO2 shows upwards of 200 connections being created per page request. Most connections exist for 60 seconds then close without...
10
by: rich | last post by:
I have a PHP5 application that accepts external messages, and inserts them into a database. It's rather high traffic.. the server could be receiving thousands at a time, and recently mysql has...
3
by: Abhishek Tripathi | last post by:
Hi, I am using a code similar to Database db = DatabaseFactory.CreateDatabase("MyDB") in every function of my DAL that interacts directly to database. But recently I have started getting...
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
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...
0
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...

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.