Accessing Session variables when using SQL Server for Session state

Discussion in 'ASP .Net' started by Michael, Dec 30, 2005.

  1. Michael

    Michael Guest

    Hi,

    If I use SQL Server to store session state, whenever I call
    Session["myvariable"] is there a call to SQL Server? Even if I call
    Session["myvariable"] a few times in the same Web page, or even the same
    code block?

    I'm asking because if there is one SQL Server call every time I have
    Session["myvariable"], then I should store that in a local variable, right?

    Thanks,
    Michael
     
    Michael, Dec 30, 2005
    #1
    1. Advertising

  2. Michael

    Marina Guest

    I'm fairly certain that session state is retrieved once at the beginning of
    the request, and then saved back to the database when the request if
    finished.

    However, this is very easily tested by running a trace on the SQL server in
    question and seeing when state is read/written to the database.

    "Michael" <> wrote in message
    news:...
    > Hi,
    >
    > If I use SQL Server to store session state, whenever I call
    > Session["myvariable"] is there a call to SQL Server? Even if I call
    > Session["myvariable"] a few times in the same Web page, or even the same
    > code block?
    >
    > I'm asking because if there is one SQL Server call every time I have
    > Session["myvariable"], then I should store that in a local variable,
    > right?
    >
    > Thanks,
    > Michael
    >
     
    Marina, Dec 30, 2005
    #2
    1. Advertising

  3. Once at the same time in the request cycle and saves
    it (whether there were changes or not) when the response cycle
    ends. It does not make a database call each time
    you change a session variable.

    If you are working in a web farm, this product is
    far more preferrable than storing state in SQL server:

    http://www.eggheadcafe.com/articles/scaleout_server.asp

    --
    Robbe Morris - 2004/2005 Microsoft MVP C#
    http://www.eggheadcafe.com/forums/merit.asp





    "Michael" <> wrote in message
    news:...
    > Hi,
    >
    > If I use SQL Server to store session state, whenever I call
    > Session["myvariable"] is there a call to SQL Server? Even if I call
    > Session["myvariable"] a few times in the same Web page, or even the same
    > code block?
    >
    > I'm asking because if there is one SQL Server call every time I have
    > Session["myvariable"], then I should store that in a local variable,
    > right?
    >
    > Thanks,
    > Michael
    >
     
    Robbe Morris [C# MVP], Dec 30, 2005
    #3
  4. Michael

    Jim Cheshire Guest

    Michael wrote:
    >
    > If I use SQL Server to store session state, whenever I call
    > Session["myvariable"] is there a call to SQL Server? Even if I call
    > Session["myvariable"] a few times in the same Web page, or even the
    > same code block?
    >
    > I'm asking because if there is one SQL Server call every time I have
    > Session["myvariable"], then I should store that in a local variable,
    > right?


    We make one call on the request as long as Session state is enabled for that
    page. It's worth noting that because of this, you can gain significant
    performance increases by disabling Session on pages that don't use it and by
    setting Session to ReadOnly on pages that have no need to write to a Session
    variable.

    --
    Jim Cheshire
    ================================
    Blog: http://blogs.msdn.com/jamesche

    Latest entry:
    Getting the PID and TID of a COM Call

    Describes how to get the PID of the
    dllhost process a COM call is executing
    in and how to locate the thread as well.
     
    Jim Cheshire, Dec 30, 2005
    #4
    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. shamanthakamani
    Replies:
    1
    Views:
    3,499
    Natty Gur
    Nov 20, 2003
  2. Not Liking Dot Net Today
    Replies:
    0
    Views:
    616
    Not Liking Dot Net Today
    Apr 21, 2004
  3. Maciek
    Replies:
    0
    Views:
    8,256
    Maciek
    Sep 15, 2005
  4. Harry Haller
    Replies:
    0
    Views:
    1,661
    Harry Haller
    Nov 7, 2006
  5. sunkrajesh
    Replies:
    0
    Views:
    356
    sunkrajesh
    May 31, 2009
Loading...

Share This Page