473,503 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlConnection Object in Application Variable OR Session Variable OR open/close

Hi and regards

What is best method to have an connection to sql, iam using sqlserver

1. have an sqlconnection open when the application start event in
global.asax so that i dont have to open it in each method where i
require to open an connection

2. open an connection in seesion so that, it is available in session of
the user thorugh out

3. open an connection in each method and close after the requried
operation is completed

which is better

as per the 3rd method ... doest openning and closing in each method is
a bad??

Comments expected

Aug 31 '06 #1
3 1924
1. This is the most horrible method. All users would share the same
connection object, which means, you are going to have multiple threads
trying to use it at once, which means your app will crash instance under any
kind of load.

2. This is slightly better, but still really bad. First off, it means you
have an open connection the entire lifetime of a session. This severely
limits the number of concurrent users, and performs badly.

3. This is how you should handle it. Connections are pooled by default,
opening and closing them is relatively inexpensive. Additionally, you will
get much better performance by not keeping unecessary connections open, 99%
of which time they are not being used.

So yes, #3 is the way to go.

"pratham" <pr************@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Hi and regards

What is best method to have an connection to sql, iam using sqlserver

1. have an sqlconnection open when the application start event in
global.asax so that i dont have to open it in each method where i
require to open an connection

2. open an connection in seesion so that, it is available in session of
the user thorugh out

3. open an connection in each method and close after the requried
operation is completed

which is better

as per the 3rd method ... doest openning and closing in each method is
a bad??

Comments expected

Aug 31 '06 #2
Thanks Marina for that fast and detailed reply Cheers :)

Marina Levit [MVP] wrote:
1. This is the most horrible method. All users would share the same
connection object, which means, you are going to have multiple threads
trying to use it at once, which means your app will crash instance under any
kind of load.

2. This is slightly better, but still really bad. First off, it means you
have an open connection the entire lifetime of a session. This severely
limits the number of concurrent users, and performs badly.

3. This is how you should handle it. Connections are pooled by default,
opening and closing them is relatively inexpensive. Additionally, you will
get much better performance by not keeping unecessary connections open, 99%
of which time they are not being used.

So yes, #3 is the way to go.

"pratham" <pr************@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Hi and regards

What is best method to have an connection to sql, iam using sqlserver

1. have an sqlconnection open when the application start event in
global.asax so that i dont have to open it in each method where i
require to open an connection

2. open an connection in seesion so that, it is available in session of
the user thorugh out

3. open an connection in each method and close after the requried
operation is completed

which is better

as per the 3rd method ... doest openning and closing in each method is
a bad??

Comments expected
Aug 31 '06 #3


Correct (on an earlier post) that #1 is really bad. #2 isn't good either.
An environment like the web, you especially want to

"Open Late" and "Close asap" model.

Open and close as fast as you can.


"pratham" <pr************@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Hi and regards

What is best method to have an connection to sql, iam using sqlserver

1. have an sqlconnection open when the application start event in
global.asax so that i dont have to open it in each method where i
require to open an connection

2. open an connection in seesion so that, it is available in session of
the user thorugh out

3. open an connection in each method and close after the requried
operation is completed

which is better

as per the 3rd method ... doest openning and closing in each method is
a bad??

Comments expected

Aug 31 '06 #4

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

Similar topics

1
558
by: Donnie Darko | last post by:
I'm trying to understand SqlConnection(), SqlCommand() For example. I use SqlConnection() as a parameter when I create a new SqlCommand. Then I open SqlConnection(), then I execute the...
16
13956
by: ed_p | last post by:
Hello, I have implemented the singleton pattern for a class to hold a SqlConnection object that will be used thruout the application to create commands. My application is a simple Windows Form...
11
2854
by: Bob | last post by:
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to various queries. The thinking behind is that this...
10
1312
by: bnob | last post by:
Im my ASP.net project I do a lot of connection to the SQL Server Database. I use a global variabe for my connection declare in a module (VB.Net code behind) All is ok but when I start a form...
4
2045
by: Steve Richter | last post by:
I really miss c++ .... I have an SqlConnection object within my Web.UI.Page object. The thinking is that the connection to the database server is opened once when the page starts to do its...
4
3877
by: Victor | last post by:
Hi Guys I have a problem here. I want to improve the performance for a website. When I looked into the system, I have found that the system made the "SqlConnection Object" static. That mean only...
5
3416
by: fniles | last post by:
I am using VB.NET 2003. When using SQLClient.SQLConnection with SQL 2005 database, 1. connection pooling is automatically used, right ? I mean, in the connection string I do not need to explicitly...
4
3415
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I am currently developing my first webapplication using .net (2.x) In a couple examples seen in the helpfile that came with VS2008 i see that they are opening a connection to the sql...
1
1366
by: Scott M. | last post by:
Many methods return objects when they are called. With Excel these objects are placed in memory and must be destroyed via ReleaseComObject as you have done with your NAR method, but the line: ...
0
7205
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,...
0
7093
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
7348
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...
0
7467
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...
0
5592
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,...
1
5021
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
3175
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...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
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...

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.