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

Static vs. non-static connection

Hi,

I`m creating a asp.net intranet system, and would like to put all the
logic of the system in a single dll. That is, when i need to create an
user for example, i would use "User.Check()" and "User.Add()" instead of
putting "insert" sql statements in the page.

Considering he following logic:

User u = new User();

u.Name = "John Doe";
u.Email = "jo*****@blah.org";
u.Login = "john";
u.Pass = "blah";

if (!User.Exists(u) and User.IsValid(u))
User.Add(u);
User.Exists checks if there is a user with that login, User.IsValid
checks to see if the name and other data is valid.. and User.Add adds
the user to the database. All the three methods access the database and
i'm creating a method that will return this database connection.

My question is: What is the best practice? Should i create a method to
return a new SqlConnection object, open and close that conection in each
method i use it, or create a property that returns a static opened
connection? Even with connection pooling, i think there is a better way
to do things than opening and closing a connection 5 or 6 times in a
single page.

Other idea was open a single static connection once, let`s say in
global.asax and using it for every everywhere. Is there any problem with
this?

Thanks.
Nov 18 '05 #1
8 1583
A static connection is shared by all users.

You could create the connection once for the lifetime of the request (and
check first if opening/closing the connection on a per method basis is a
real performance hit).

Patrice
"Natan" <nv***@mandic.com.br> a écrit dans le message de
news:%2*****************@TK2MSFTNGP09.phx.gbl...
Hi,

I`m creating a asp.net intranet system, and would like to put all the
logic of the system in a single dll. That is, when i need to create an
user for example, i would use "User.Check()" and "User.Add()" instead of
putting "insert" sql statements in the page.

Considering he following logic:

User u = new User();

u.Name = "John Doe";
u.Email = "jo*****@blah.org";
u.Login = "john";
u.Pass = "blah";

if (!User.Exists(u) and User.IsValid(u))
User.Add(u);
User.Exists checks if there is a user with that login, User.IsValid
checks to see if the name and other data is valid.. and User.Add adds
the user to the database. All the three methods access the database and
i'm creating a method that will return this database connection.

My question is: What is the best practice? Should i create a method to
return a new SqlConnection object, open and close that conection in each
method i use it, or create a property that returns a static opened
connection? Even with connection pooling, i think there is a better way
to do things than opening and closing a connection 5 or 6 times in a
single page.

Other idea was open a single static connection once, let`s say in
global.asax and using it for every everywhere. Is there any problem with
this?

Thanks.

Nov 18 '05 #2
Patrice wrote:
A static connection is shared by all users.

You could create the connection once for the lifetime of the request (and
check first if opening/closing the connection on a per method basis is a
real performance hit).


I thought about this, but how? I would need to pass the connection to
every method i use...

does anyone know a better way?
Nov 18 '05 #3
Try adding the connection to the Context collection, this should be
available in every method. Make sure you close it after everyone is done
with it.

"Natan" <nv***@mandic.com.br> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Patrice wrote:
A static connection is shared by all users.

You could create the connection once for the lifetime of the request (and check first if opening/closing the connection on a per method basis is a
real performance hit).


I thought about this, but how? I would need to pass the connection to
every method i use...

does anyone know a better way?

Nov 18 '05 #4
Thanks! I didn`t know about this collection! Thanks a lot!

Marina wrote:
Try adding the connection to the Context collection, this should be
available in every method. Make sure you close it after everyone is done
with it.

Nov 18 '05 #5
Best practice is NOT to instantiate a single connection and keep it open for multiple users or multiple queries, etc. The way connection pooling works is that the allocation of actual connections is managed for you. If many sessions need DB access, multiple connections are allocated and shared, as needed. As long as the connection strings are IDENTICAL, you will get optimized allocation for multiple users. (So, use a single user account for all database access, unless different users - or different application functions - require different permissions within the DB itself. If that's the case, the multple connection pools will be allocated for the different connection strings.

Keeping a connection open for longer than it absolutely has to be could, in effect, tie up an actual physical connection longer than necessary - making it unavaliable for the next command, or another command in another session. So, the only thing you have to persist (in an application variable - or wherever) is the connection string, not the connection

Open as late as possible, close as soon as possible. It's that easy - don't try to defeat the purpose of connection pooling
It's that easy - don't try to get
Nov 18 '05 #6
How many users are you signing on at once for that to be a performance
consideration?

Nov 18 '05 #7
spamfurnace wrote:
How many users are you signing on at once for that to be a performance
consideration?


There is no heavy traffic, I just wondered what was the best way to do
it. I think using Context collection to store the connection is the best
way. This way I open one connection per request, use it everywhere and
then close it, like in old asp.
Nov 18 '05 #8

"Natan" <nv***@mandic.com.br> wrote in message
news:e%****************@tk2msftngp13.phx.gbl...
spamfurnace wrote:
How many users are you signing on at once for that to be a performance
consideration?


There is no heavy traffic, I just wondered what was the best way to do
it. I think using Context collection to store the connection is the best
way. This way I open one connection per request, use it everywhere and
then close it, like in old asp.


There's one caveat though - if you do anything during the request processing
that may block for a while (e.g., call a web service or read a file) then
the database connection will be in use unnecessarily. This will reduce the
number of concurrent requests you can serve. In this case, opening and
closing the database connection whenever you need it is better than storing
it (and keeping it open) in the HttpContext.

Sami
Nov 18 '05 #9

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

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...
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
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...

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.