473,652 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1322
Close is enough, that will release the connection back into the pool.

You do not need to call dispose.

<an*******@disc ussions.microso ft.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.D ispose() calls SqlConnection.C lose() 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*******@disc ussions.microso ft.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.D ispose() calls SqlConnection.C lose() 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*******@disc ussions.microso ft.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
2587
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 to disk with no missing data). I've recently started do the same thing with database connections. I'm wondering if anyone has had trouble with this sort of "lazy" programming style. My assumption is the the database connection will get closed...
5
14214
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 mission critical roll in our organization, so I'd like some comments on what others think of this database platform. Mainly I'm curious how easy/difficult it is to query a Cache Database, and does it use standard SQL calls like Oracle and MS SQL? ...
14
7129
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 is just the mdb, not an mde. FE and BE are in different directories on the same machine. Are there settings, preferably via code, that I can add to stop this error? Thank you for any help.
5
886
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 need to create a dispose method?
12
2109
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 unmanaged resources such as file handles or database connections that must be released when the managed object that uses them is reclaimed."
17
2556
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 keep it up to date, but that could probably be done with a macro. At the moment I'm just considoring possible options, so just need to know if it can be done, how easy and a rough idea of how.
4
1959
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 being reused. A few connections are reused. SQL System Profiler shows many “Audit Login” and many “RPC: Completed” records for each page request – often involving the exact same SQL statement called in exactly the same manner from within a...
10
1721
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 been locking up because it's exceeding the max_connections limit. I raised it for now, but that's only a temporary fix. My question is is there a way to make a connection to mysql that multiple instances of an object will use together? I'm under...
3
3004
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 connection pool errors. I think, it is because of incorrect usage pattern of Database Object of DAAB. I want to know that what should be the best approach to it. Shall
0
8370
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8283
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
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
7302
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 projectplanning, coding, testing, and deploymentwithout 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...
1
6160
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
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...
1
2707
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
1
1914
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.