Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Static vs. non-static connection

Reply
Thread Tools

Static vs. non-static connection

 
 
Natan
Guest
Posts: n/a
 
      05-24-2004
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 = "(E-Mail Removed)";
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.
 
Reply With Quote
 
 
 
 
Patrice
Guest
Posts: n/a
 
      05-24-2004
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" <(E-Mail Removed)> a écrit dans le message de
news:%23%(E-Mail Removed)...
> 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 = "(E-Mail Removed)";
> 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.



 
Reply With Quote
 
 
 
 
Natan
Guest
Posts: n/a
 
      05-24-2004
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?
 
Reply With Quote
 
Marina
Guest
Posts: n/a
 
      05-24-2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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?



 
Reply With Quote
 
Natan
Guest
Posts: n/a
 
      05-24-2004
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.

 
Reply With Quote
 
=?Utf-8?B?UmljaA==?=
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
spamfurnace
Guest
Posts: n/a
 
      05-25-2004
How many users are you signing on at once for that to be a performance
consideration?



 
Reply With Quote
 
Natan
Guest
Posts: n/a
 
      05-25-2004
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.
 
Reply With Quote
 
Sami Vaaraniemi
Guest
Posts: n/a
 
      05-26-2004

"Natan" <(E-Mail Removed)> wrote in message
news:e%(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 2 05-26-2005 06:25 PM
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 0 05-26-2005 11:07 AM
Static is REALLY Static! Paul W ASP .Net 2 05-03-2005 10:12 AM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM
Why do static and non-static method names collide? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 12 07-05-2003 04:59 PM



Advertisments