Session State Module

Session are maintained at controller level. In .net Session state is managed by the SessionStateModule class, which calls the session-state store provider to read and write session data to the data store at different times during a request. At the beginning of a request, the SessionStateModule instance retrieves data from the data source by calling the GetItemExclusive method. Exclusive means when one is reading or writing don’t allow other to read/write simultaneously.

There are various types of Session State available:

  1.  Default
    • Whenever session state is not mentioned explicitly, it is treated as default. In Default, whenever any read/write operation is performed on session then application locks the session object i.e. only one request can read/write the session at any given point of time.
  2. Disable
    •  When session state is set to Disable then no request can neither read  nor write the session.  If any request tries to read/write the session then application will throw the “Null Reference”  exception.
  3.  Read-Only
    • As name says Read-only, many will consider that session is Ready-only i.e no request can update(write) the session. But this is wrong. If any request tries to change the session object no error will be thrown, it will simply update the session. So don’t get confused with name.
    • The Read-only follows the reader-writer concept i.e if 1st request is for read(read-only), then session get locked by that request until that request gets completed. But if another request came for read(read-only) in parallel with current request then application will allow that request to read. Which means all the subsequent request for read(read-only) will be granted. If any request came for write (Default) then that write request has to wait until all read request are finished. If write request is in progress then all read has to wait.
    • Also any read-only request can update the session. But the updated session will only be persisted until that request is not finished and will not affect global session value. In simple terms, the updated session value will be local to that request.

For example these mode see my next upcoming post. Leave you comment.