使用SQL Server儲存ASP.NET Session變數

iSQlServer發表於2009-02-13

建立和配置ASP.NET Session狀態資料庫

在基於NLB(網路負載平衡)環境下的ASP.NET Web應用程式開發,我們需要將Session儲存在資料庫中供多個Web應用程式呼叫,以下為配置方法及注意事項。

1.建立用於儲存ASP.NET Session的資料庫(遠端、本地皆可,使用資料庫使用者身份認證)
在Windows\Microsoft.NET\Framework/V2.0.50727目錄下使用如下命令:
aspnet_regsql.exe -S -U -P -E -ssadd -sstype c -d
命令執行後就會成功建立起用於儲存ASP.NET Session變數的資料庫了。

2.Web.Config檔案配置項
我們需要在ASP.NET Web應用程式中的Web.Config檔案修改sessionState配置項以使Session狀態資料庫生效。
配置節點如下:
            sqlConnectionString="server=;database=;uid=;pwd=;"
 allowCustomSqlDatabase="True"
            cookieless="false"
            timeout="20" />

3.注意在進行系統測試(主要是負載測試)的時候,因為資料庫訪問負載的增加,需要調整SQL Server相應超時的配置項以適應負載。(預設值為10,請適度進行調整。)

ASP.NET Session狀態資料庫資料模型
1.ASPStateTempSessions表定義

列名 型別 描述
SessionId nvarchar(88) Session ID + application ID
Created datetime Date and time session was created (UTC)
Expires datetime Date and time session expires (UTC)
LockDate datetime UTC date and time session was locked
LockDateLocal datetime Local date and time session was locked
LockCookie int Lock ID
Timeout int Session timeout in minutes
Locked bit 1=Session locked, 0=Session not locked
SessionItemShort varbinary(7000) Serialized session state (if <= 7,000 bytes)
SessionItemLong image Serialized session state (if > 7,000 bytes)
Flags int Session state flags (1=Uninitialized session)

2.ASPStateTempApplications表定義
列名 型別 描述
AppId int Application ID
AppName char(280) Application name


3.使用的儲存過程

Stored Procedure Description
CreateTempTables Creates the ASPStateTempSessions and ASPStateTempApplications tables; called during setup, but not called by SqlSessionStateStore.
DeleteExpiredSessions Used by SQL Server Agent to remove expired sessions.
GetHashCode Hashes an application name and returns the hash; called by TempGetAppID.
GetMajorVersion Returns SQL Server's major version number.
TempGetAppID Converts an application name into an application ID; queries the ASPStateTempApplications table and inserts a new record if necessary.
TempGetStateItem Retrieves read-only session state from the database (ASP.NET 1.0; ASP.NET 1.1/SQL Server 7).
TempGetStateItem2 Retrieves read-only session state from the database (ASP.NET 1.1).
TempGetStateItem3 Retrieves read-only session state from the database (ASP.NET 2.0).
TempGetStateItemExclusive Retrieves read/write session state from the database (ASP.NET 1.0; ASP.NET 1.1/SQL Server 7).
TempGetStateItemExclusive2 Retrieves read/write session state from the database (ASP.NET 1.1).
TempGetStateItemExclusive3 Retrieves read/write session state from the database (ASP.NET 2.0).
TempGetVersion Marker whose presence indicates to ASP.NET 2.0 that the session state database is ASP.NET 2.0-compatible.
TempInsertStateItemLong Adds a new session, whose size is > 7,000 bytes, to the database.
TempInsertStateItemShort Adds a new session, whose size is <= 7,000 bytes, to the database.
TempInsertUninitializedItem Adds a new uninitialized session to the database in support of cookieless sessions.
TempReleaseStateItemExclusive Releases a lock on a session; called when ASP.NET determines that a request has timed out and calls the provider's ReleaseItemExclusive method.
TempRemoveStateItem Removes a session from the database when the session is abandoned.
TempResetTimeout Resets a session's timeout by writing the current date and time to the corresponding record's Expires field.
TempUpdateStateItemLong Updates a session whose size is > 7,000 bytes.
TempUpdateStateItemLongNullShort Updates a session whose old size is <= 7,000 bytes, but whose new size is > 7,000 bytes.
TempUpdateStateItemShort Updates a session whose size is <= 7,000 bytes.
TempUpdateStateItemShortNullLong Updates a session whose old size is > 7,000 bytes, but whose new size is <= 7,000 bytes.

ASP.NET 狀態資料庫FAQ

1.如果把SESSION值存放到資料庫中去,使用者關閉了程式那怎麼樣清空資料庫裡的SESSION值呢?
   實際ASP.NET在建立狀態資料庫的時候會在SQL Server代理(SQL Server Agent)的作業中新增一個作業,名稱為_Job_DeleteExpiredSessions。如果開啟SQL Server代理服務資料庫可以通過新增的狀態記錄的超時時間欄位(Exprires)定期對超時的狀態資料進行刪除。

2.ASPStateTempSessions表中的SessionId欄位如何使用?
資料庫中此表的SessionID欄位的值,由SessionID和AppID共同組成,最後8位為AppID所以,後8位之前一定是SessionID。例如,儲存在資料庫中的值為"ekr30c3mwvnc3145yrswew3a037e5e5a",後8位的"037e5e5a"為AppID,而前面的"ekr30c3mwvnc3145yrswew3a"為應用程式中你可以使用Session.SessionID獲得的字串。

3.如何判斷Session何時被更新的?
Session記錄被更新時會同時更新Expires和LockDateLocal,Expires欄位為UTC時間,如果想通過本地之間進行比較判斷還是需要使用LockDateLocal。

4.獲得Web.config配置檔案節點資訊的程式?
使用SQL Server儲存ASP.NET Session變數
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt''獲得Web.config檔案配置例項
Dim configuration As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/web.config")

''獲得狀態配置節點例項
Dim mSessionStateSection As System.Web.Configuration.SessionStateSection = CType(configuration.GetSection("system.web/sessionState"),System.Web.Configuration.SessionStateSection)

''獲得狀態模式
Response.Write(mSessionStateSection.Mode)
''獲得狀態超時時間
Response.Write(mSessionStateSection.Timeout)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-551828/,如需轉載,請註明出處,否則將追究法律責任。

相關文章