Is access to the Session thread-safe?

Discussion in 'ASP .Net' started by Chris Newby, Mar 28, 2005.

  1. Chris Newby

    Chris Newby Guest

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

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

    Is access to MyObject thread-safe?
     
    Chris Newby, Mar 28, 2005
    #1
    1. Advertising

  2. Chris Newby

    Scott Allen Guest

    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"
    <> wrote:

    > When accessing, for example, an object stored in the session such as:
    >
    >Session[ "MyObject" ].MyProperty = "Some Value";
    >
    >Is access to MyObject thread-safe?
    >
     
    Scott Allen, Mar 28, 2005
    #2
    1. Advertising

  3. Chris Newby

    Chris Newby Guest

    Yeah, makes perfect sense.

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


    "Scott Allen" <> wrote in message
    news:eek:...
    > 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"
    > <> wrote:
    >
    > > When accessing, for example, an object stored in the session such as:
    > >
    > >Session[ "MyObject" ].MyProperty = "Some Value";
    > >
    > >Is access to MyObject thread-safe?
    > >

    >
     
    Chris Newby, Mar 28, 2005
    #3
  4. Chris Newby

    Scott Allen Guest

    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"
    <> wrote:

    >Yeah, makes perfect sense.
    >
    >Could you possibly elaborate a little more on exactly when ASP.NET creates
    >and releases the ReaderWriter Lock?
    >
    >
     
    Scott Allen, Mar 28, 2005
    #4
  5. Chris Newby

    Chris Newby Guest

    Thanks Scott, very helpful//

    "Scott Allen" <> wrote in message
    news:...
    > 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"
    > <> wrote:
    >
    > >Yeah, makes perfect sense.
    > >
    > >Could you possibly elaborate a little more on exactly when ASP.NET

    creates
    > >and releases the ReaderWriter Lock?
    > >
    > >

    >
     
    Chris Newby, Mar 28, 2005
    #5
    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. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    Thread safe singleton to access the cache?

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    789
    Bruce Barker
    Oct 6, 2005
  2. Adam Warner
    Replies:
    13
    Views:
    850
    Patricia Shanahan
    Mar 28, 2006
  3. Gabriel Rossetti
    Replies:
    0
    Views:
    1,329
    Gabriel Rossetti
    Aug 29, 2008
  4. Mats Olsson
    Replies:
    9
    Views:
    381
    Just1Coder
    Sep 14, 2004
  5. John Nagle
    Replies:
    5
    Views:
    475
    John Nagle
    Mar 12, 2012
Loading...

Share This Page