Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Statics and connections

Reply
Thread Tools

Statics and connections

 
 
Jason
Guest
Posts: n/a
 
      12-06-2004
Hi

I have a web app that connects to a SQL 2000 server using SqlConnection
class.
my question is this, can i put this SqlConnection into a static variable
somewhere, so that i can initialise it once off and use it for all
connections thereafter?
Basically, would a static SqlConnection variable be problematic in a web
application?

Thanks
Jason


 
Reply With Quote
 
 
 
 
Peter Strĝiman
Guest
Posts: n/a
 
      12-06-2004
Don't do that.

If you put that in a static variable, only one connection would exist. If
two people tried to access a web page at the same time, both requests would
use the same database connection, and that would be problematic. E.g. only
one data reader can be supported pr. connection.

So create your connections when you need them and dispose them immediately
when you're done with them. The .NET framework implements a connection pool
that can cache you connections. So even though you create a new connection
in each request, it may still be the same connection that gets reused.

If you're using C# you can use the "using" statement to ensure your
connection is disposed.
using( SqlConnection connection = new SqlConnection( "..." )
{
connection.Open();
...
}

When your code leaves the block, the connection is disposed. That is no
matter whether an exception is thrown, you explicitly return from the
function (calling return), or the code leaves the block normally.

If you're using VB, you can use the try-finally statemene (has exactly the
same effect as the C# example, just uses more lines - the finally block is
always executed)

dim connection as new SqlConnection("...")
try
connection.Open
...
finally
connection.Dispose
end try


"Jason" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi
>
> I have a web app that connects to a SQL 2000 server using SqlConnection
> class.
> my question is this, can i put this SqlConnection into a static variable
> somewhere, so that i can initialise it once off and use it for all
> connections thereafter?
> Basically, would a static SqlConnection variable be problematic in a web
> application?
>
> Thanks
> Jason
>
>



 
Reply With Quote
 
 
 
 
Jason
Guest
Posts: n/a
 
      12-06-2004
excellent thanks.

very helpful, and very quick response!

Thanks again
"Peter Strĝiman" <blah@blahblahblah> wrote in message
news:%(E-Mail Removed)...
> Don't do that.
>
> If you put that in a static variable, only one connection would exist. If
> two people tried to access a web page at the same time, both requests

would
> use the same database connection, and that would be problematic. E.g. only
> one data reader can be supported pr. connection.
>
> So create your connections when you need them and dispose them immediately
> when you're done with them. The .NET framework implements a connection

pool
> that can cache you connections. So even though you create a new connection
> in each request, it may still be the same connection that gets reused.
>
> If you're using C# you can use the "using" statement to ensure your
> connection is disposed.
> using( SqlConnection connection = new SqlConnection( "..." )
> {
> connection.Open();
> ...
> }
>
> When your code leaves the block, the connection is disposed. That is no
> matter whether an exception is thrown, you explicitly return from the
> function (calling return), or the code leaves the block normally.
>
> If you're using VB, you can use the try-finally statemene (has exactly the
> same effect as the C# example, just uses more lines - the finally block is
> always executed)
>
> dim connection as new SqlConnection("...")
> try
> connection.Open
> ...
> finally
> connection.Dispose
> end try
>
>
> "Jason" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi
> >
> > I have a web app that connects to a SQL 2000 server using SqlConnection
> > class.
> > my question is this, can i put this SqlConnection into a static variable
> > somewhere, so that i can initialise it once off and use it for all
> > connections thereafter?
> > Basically, would a static SqlConnection variable be problematic in a web
> > application?
> >
> > Thanks
> > Jason
> >
> >

>
>



 
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
global variables and class statics in a static library rsforster@sympatico.ca C++ 3 09-09-2006 05:30 PM
lifetime of global statics vs. statics in functions Stuart MacMartin C++ 5 07-27-2005 04:19 PM
Intialisation order and statics Spacen Jasset C++ 6 01-31-2005 09:08 PM
PIX 515 with statics and ACLs blocks dmz to outside access SuperIce Cisco 2 10-01-2004 05:11 PM
pix, vpn and statics P Cisco 1 08-06-2004 08:54 AM



Advertisments