Handling Session Efficiently Using SQLSERVER State Management in .NETIn general, the application you develop is a logical unit where in the data fed in one page is required in the next page for further processing. The data has to be accessible across pages. This is possible through state management. But how do
you accomplish that? Does your web application support this feature by
default? To your surprise, the answer is NO. You will be using HTTP Protocol
for your web application. This protocol is stateless. It doesnt
relate requests. Each request is distinct. It receives request, processes
it. After processing it discards all the data involved in processing.
How to establish state management then? State Management is established using Sessions. Data is different for different Users. Each Users data across server calls are managed in sessions. To make things much clearer for you, an example in C# is given below: Storing Data
in Session is done as follows: Now control
is redirected to the next page: In NextForm.aspx,
you can display the session data: The concept is going to be the same in VB.NET. Syntax alone varies slightly. Instead of Session[UserName], you will be using Session(UserName). Retaining Session Object is termed as Session State Management. This can be accomplished in three different ways: In
Process: This is the default state management with high performance.
The state information is stored in memory. Hope you have got a basic idea on what state management is. With this, let us get into more details on SQL Server State Management. In SQL Server state management, data is stored in SQL Server database. In In-Process and State Server, data is not stored persistently. But here Persistent Storage is possible. The following diagram will give you an outline on SQL Server State Management.
As the above diagram depicts, SQL Server State Management can be performed in two ways. In case of persistent data storage, data is stored in a permanent database called ASPState. State tables are created in the ASPState Database when the script InstallPersistSQLState.sql is executed. You can remove State tables from the database by executing UninstallPersistSQLState.sql script. In non-persistent/temporary data storage, the state tables are created in tempdb which is a temporary database. Tables are created when InstallSQLState.sql script is executed. This Script creates a job called ASPState_Job_DeleteExpired_Sessions which will delete data corresponding to expired sessions. This job will run every minute as long as your SQLServerAgent service is running. Drawback in this non-persistent data storage is that the state tables will be lost if the SQL Server is restarted. Because tempdb database is in temporary storage area which gets cleared every time the server is restarted. To establish SQL Server State Management, you have to perform the following steps: Step1:
Run either InstallPersistSQLState.sql or InstallSQLState.sql as per your
need. The script will be available in <Root Folder>\Microsoft.NET\Framework\<version>.
The script can be executed using any SQL Server Utility. You can execute
the script in SQL Query Analyzer by doing the following: Step2:
Make the following entry in web.config file of your application: Ensure that SQLServerAgent is running. If it is not running then session timeout will not happen and data will still remain in the database. Though SQL
Server State Management is advantageous in terms of providing persistent
storage of state, there are few limitations to it as well. They are listed
as below: In spite of all these drawbacks, it is the most reliable and commonly used way of state management.
|