Showing posts with label Session. Show all posts
Showing posts with label Session. Show all posts

what is the difference between session.abandon() and session.clear() ?

 Session.Abandon():

          Session.Abandon() destroys the session
       Abandon will destroy the session completely, meaning that you need to begin a new session before you can store any more values in the session for that user.
 Session.Clear():

Session.Clear() just removes all values

                Clearing the session will not unset the session, it still exists with the same ID for the user but with the values simply cleared.
The Clear method removes all keys and values from the current session. Compare to Abandon method, it doesn’t create the new session, It just make all variables in session to NULL.


Diff B/w Session.Clear() and Session.RemoveAll():

actually there is no difference. Session.RemoveAll() methods internally makes a call to Clear() method only.


Short note on Asp.net session modes ?


In ASP.NET there are following session mode available, 
  • InProc
  • StateServer
  • SQLServer
  • Custom 
  • The In-Process(InProc) Session provider is the fastest method, because  of  everything stored  inside the memory. Session data will be loss if we restart web server  or if Worker Process Recycles. You can use in small web application where number of users are less. Do not use InProc in Web Farm.
  • In StateServer Session modes Session data maintain by aspnet_state.exe. Its keeps session data out of Web server. So any  issue with web server does not affect  session data. You need to Serialized object before storing data  in StateServer Session. we can use it in web farm also.
  • SQLServer Session modes store data in SQL Server, we need to provide the connection string. Here we also need to serialize the data before storing it to session. This is very useful in production environment with web farm mode.
  • we can use Custom provider for custom data source or when we need to use some existing table to store session data. We can also create our custom sessionID in Custom mode. But it is not recommended to create your own custom provider. Its recommended to use any third party provider.

Enabling and Disabling Session in asp.net:


For performance optimization we can enable or disable session. because each and every page read and write access of the page, and this involves some performance overhead. So its always better to enable and disable session based on requirements rather than make it enable always. we can  enable and disable the session State in two ways: 
  • Page Level 
  • Application Level  
Page Level :
we can disable session state in page level using EnableSessionState attributes with in Page directive. 
This will disable the session activities for that particular page
Same way we can make it read only also , It will permit to access session data, but it will not allow writing data on session. 
Application Level : 
Session state can be disabled for all over the web application using EnableSessionState property inWeb.Config .
Generally we are use Page level, because some of page may not require any session data or may be only read the session data.

Removing Session From Session Variable in asp.net


Following are the list of methods that are used to removing the session .

Method
Description
Session.Remove(strSessionName);
Remove an Item from Session State Collection
Session.RemoveAll()
Remove all items from session collection 
Session.Clear()
Remove all items from session collection  Note: There is no difference between Clear and RemoveAll. RemoveAll() calls Clear(), internally.
Session.Abandon()
Cancels the Current Session