469,926 Members | 2,069 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

Is access to the Session thread-safe?

When accessing, for example, an object stored in the session such as:

Session[ "MyObject" ].MyProperty = "Some Value";

Is access to MyObject thread-safe?
Nov 19 '05 #1
4 2726
Hi Chris:

Here are some details:

ASP.NET does manage access to session state with a ReaderWriterLock.
The default is to take a writer lock, which serializes requests coming
in on the same session (using EnableSessionState="ReadOnly" takes a
reader lock and does allow concurrent request processing on a
session).

In this example, you are not really modifying session state, but
modifying an object through a reference stored in session state. If
anyone else has a reference to the same object the code is only thread
safe if MyObject is thread safe.

Know what I mean?

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Sun, 27 Mar 2005 19:59:26 -0500, "Chris Newby"
<CA*****************@hotmail.com> wrote:
When accessing, for example, an object stored in the session such as:

Session[ "MyObject" ].MyProperty = "Some Value";

Is access to MyObject thread-safe?


Nov 19 '05 #2
Yeah, makes perfect sense.

Could you possibly elaborate a little more on exactly when ASP.NET creates
and releases the ReaderWriter Lock?
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:om********************************@4ax.com...
Hi Chris:

Here are some details:

ASP.NET does manage access to session state with a ReaderWriterLock.
The default is to take a writer lock, which serializes requests coming
in on the same session (using EnableSessionState="ReadOnly" takes a
reader lock and does allow concurrent request processing on a
session).

In this example, you are not really modifying session state, but
modifying an object through a reference stored in session state. If
anyone else has a reference to the same object the code is only thread
safe if MyObject is thread safe.

Know what I mean?

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Sun, 27 Mar 2005 19:59:26 -0500, "Chris Newby"
<CA*****************@hotmail.com> wrote:
When accessing, for example, an object stored in the session such as:

Session[ "MyObject" ].MyProperty = "Some Value";

Is access to MyObject thread-safe?

Nov 19 '05 #3
When the SessionStateModule catches the AcquireRequestState event
during request processing. This happens before control reaches the
ASPX page handler.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sun, 27 Mar 2005 21:15:12 -0500, "Chris Newby"
<CA*****************@hotmail.com> wrote:
Yeah, makes perfect sense.

Could you possibly elaborate a little more on exactly when ASP.NET creates
and releases the ReaderWriter Lock?


Nov 19 '05 #4
Thanks Scott, very helpful//

"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:48********************************@4ax.com...
When the SessionStateModule catches the AcquireRequestState event
during request processing. This happens before control reaches the
ASPX page handler.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sun, 27 Mar 2005 21:15:12 -0500, "Chris Newby"
<CA*****************@hotmail.com> wrote:
Yeah, makes perfect sense.

Could you possibly elaborate a little more on exactly when ASP.NET createsand releases the ReaderWriter Lock?

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Andrew Chanter | last post: by
12 posts views Thread by dht | last post: by
4 posts views Thread by Al Cohen | last post: by
1 post views Thread by Christopher | last post: by
2 posts views Thread by Gavin Lyons via .NET 247 | last post: by
1 post views Thread by wekrato | last post: by
5 posts views Thread by ┼×enol Akbulak | last post: by
reply views Thread by Dmitry Bogdanov | last post: by
4 posts views Thread by Nick Gilbert | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.