Opening/closing db connection in global.asax

D

Daves

is it a good or bad practise to use the session_start & session_end for
opening & closing the database connection? I'd thought that on powerful
servers today the time the connection is kept locked should not matter as
scripts (especially .Net!) are now run in milliseconds?
 
C

Craig Deelsnyder

is it a good or bad practise to use the session_start & session_end for
opening & closing the database connection? I'd thought that on powerful
servers today the time the connection is kept locked should not matter as
scripts (especially .Net!) are now run in milliseconds?

Bad practice; as discovered elsewhere, these 2 events don't always occur
or if they do, and your session is 20 minutes, then if a user closes their
browser, the connection sits there (open) for another 19+ minutes.

ADO.NET will pool connections for you automatically, so there's no need to
do anything other than open a connection when needed and close as soon as
done.

There are some caveats to consider, though, as illustrated in this article:

http://www.ondotnet.com/pub/a/dotnet/2004/02/09/connpool.html
 
B

Brock Allen

I'd discourage this approach as it's unnecessary to keep this resource open
when most of the time you're not using it. In general you should open the
connection when you need it, use it, then close it as soon as possible. By
default (if you're using the SqlServer managed provider) connections are
pooled, so if other requests into your application are connecting to the
DB with the same connection string (same credentials, essentially) then they
piggy-back off of the open connection. Makes for better sharing of the resource
(connection).
 
M

Manohar Kamath

Actually, if the connection pooling is turned on, it does not even matter
where the connections are being opened -- since the connections are re-used.
For a good design, create a class that dispenses Connection objects, and you
can use this from anywhere.

public class ConnectionDispenser
{

// Get a new connection object
public SqlConnection GetConnection()
{
}
}
 
W

WJ

The best approach is using MS/DAAB. It manages connection for you
efficiently and bug free.

John
 
D

Daves

well the main idea is to make code shorter, it's "noisy" to have all these
open/close lines repeatedly!!

Do you know of any article on this subject? (c#)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top