Statics and connections

Discussion in 'ASP .Net' started by Jason, Dec 6, 2004.

  1. Jason

    Jason Guest

    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
     
    Jason, Dec 6, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
    >
    >
     
    Peter Strøiman, Dec 6, 2004
    #2
    1. Advertising

  3. Jason

    Jason Guest

    excellent thanks.

    very helpful, and very quick response! :)

    Thanks again
    "Peter Strøiman" <blah@blahblahblah> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
    >
     
    Jason, Dec 6, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spacen Jasset

    Intialisation order and statics

    Spacen Jasset, Jan 31, 2005, in forum: C++
    Replies:
    6
    Views:
    411
    Spacen Jasset
    Jan 31, 2005
  2. Stuart MacMartin
    Replies:
    5
    Views:
    570
    Victor Bazarov
    Jul 27, 2005
  3. Replies:
    3
    Views:
    933
    Thomas Matthews
    Sep 9, 2006
  4. jobs
    Replies:
    2
    Views:
    903
  5. Dirk Bruere at NeoPax

    Threads and statics

    Dirk Bruere at NeoPax, Apr 7, 2011, in forum: Java
    Replies:
    13
    Views:
    654
Loading...

Share This Page