使用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 -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表定义

列名类型描述
SessionIdnvarchar(88)Session ID + application ID
CreateddatetimeDate and time session was created (UTC)
ExpiresdatetimeDate and time session expires (UTC)
LockDatedatetimeUTC date and time session was locked
LockDateLocaldatetimeLocal date and time session was locked
LockCookieintLock ID
TimeoutintSession timeout in minutes
Lockedbit1=Session locked, 0=Session not locked
SessionItemShortvarbinary(7000)Serialized session state (if <= 7,000 bytes)
SessionItemLongimageSerialized session state (if > 7,000 bytes)
FlagsintSession state flags (1=Uninitialized session)

2. ASPStateTempApplications表定义
列名类型描述
AppIdintApplication ID
AppNamechar(280)Application name


3.使用的存储过程

Stored ProcedureDescription
CreateTempTablesCreates the ASPStateTempSessions and ASPStateTempApplications tables; called during setup, but not called by SqlSessionStateStore.
DeleteExpiredSessionsUsed by SQL Server Agent to remove expired sessions.
GetHashCodeHashes an application name and returns the hash; called by TempGetAppID.
GetMajorVersionReturns SQL Server's major version number.
TempGetAppIDConverts an application name into an application ID; queries the ASPStateTempApplications table and inserts a new record if necessary.
TempGetStateItemRetrieves read-only session state from the database (ASP.NET 1.0; ASP.NET 1.1/SQL Server 7).
TempGetStateItem2Retrieves read-only session state from the database (ASP.NET 1.1).
TempGetStateItem3Retrieves read-only session state from the database (ASP.NET 2.0).
TempGetStateItemExclusiveRetrieves read/write session state from the database (ASP.NET 1.0; ASP.NET 1.1/SQL Server 7).
TempGetStateItemExclusive2Retrieves read/write session state from the database (ASP.NET 1.1).
TempGetStateItemExclusive3Retrieves read/write session state from the database (ASP.NET 2.0).
TempGetVersionMarker whose presence indicates to ASP.NET 2.0 that the session state database is ASP.NET 2.0-compatible.
TempInsertStateItemLongAdds a new session, whose size is > 7,000 bytes, to the database.
TempInsertStateItemShortAdds a new session, whose size is <= 7,000 bytes, to the database.
TempInsertUninitializedItemAdds a new uninitialized session to the database in support of cookieless sessions.
TempReleaseStateItemExclusiveReleases a lock on a session; called when ASP.NET determines that a request has timed out and calls the provider's ReleaseItemExclusive method.
TempRemoveStateItemRemoves a session from the database when the session is abandoned.
TempResetTimeoutResets a session's timeout by writing the current date and time to the corresponding record's Expires field.
TempUpdateStateItemLongUpdates a session whose size is > 7,000 bytes.
TempUpdateStateItemLongNullShortUpdates a session whose old size is <= 7,000 bytes, but whose new size is > 7,000 bytes.
TempUpdateStateItemShortUpdates a session whose size is <= 7,000 bytes.
TempUpdateStateItemShortNullLongUpdates 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配置文件节点信息的程序?
ContractedBlock.gif ExpandedBlockStart.gif Code
<!--

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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/16436858/viewspace-551828/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值