Accessing Session variables when using SQL Server for Session state

M

Michael

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
 
M

Marina

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.
 
R

Robbe Morris [C# MVP]

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
 
J

Jim Cheshire

Michael said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top