使用SQL Server存储ASP.NET Session变量
創建和配置ASP.NET Session狀態數據庫
在基于NLB(網絡負載平衡)環境下的ASP.NET Web應用程序開發,我們需要將Session存儲在數據庫中供多個Web應用程序調用,以下為配置方法及注意事項。
1.創建用于存儲ASP.NET Session的數據庫(遠程、本地皆可,使用數據庫用戶身份認證) 在Windows\Microsoft.NET\Framework/V2.0.50727目錄下使用如下命令: aspnet_regsql.exe -S <SQL Server IP> -U <User Name> -P <Password> -E -ssadd -sstype c -d <Database Name> 命令執行后就會成功建立起用于存儲ASP.NET Session變量的數據庫了。
2.Web.Config文件配置項 我們需要在ASP.NET Web應用程序中的Web.Config文件修改sessionState配置項以使Session狀態數據庫生效。 配置節點如下: <sessionState mode="SQLServer" ??????????? sqlConnectionString="server=<Server IP>;database=<Database Name>;uid=<User Name>;pwd=<Password>;" 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) |
| AppId | int | Application ID |
| AppName | char(280) | Application name |
?
3.使用的存儲過程
| 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配置文件節點信息的程序?
?
?
Code ''獲得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)
?
轉載于:https://www.cnblogs.com/zxktxj/archive/2012/03/09/2387416.html
總結
以上是生活随笔為你收集整理的使用SQL Server存储ASP.NET Session变量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用BAT批处理执行sql
- 下一篇: 设计模式第三集——装饰者模式(Decor