创建和配置ASP.NET Session状态数据库
在基于NLB(网络负载平衡)环境下的ASP.NET Web应用程序开发,我们需要将Session存储在数据库中供多个Web应用程序调用,以下为配置方法及注意事项。
1.创建用于存储ASP.NET Session的数据库(远程、本地皆可,使用数据库用户身份认证)
在Windows\Microsoft.NET\Framework/V2.0.50727目录下使用如下命令:

命令: aspnet_regsql.exe -S QQ442518843 -U sa -P Admin123456  -ssadd -sstype c -d SessionDemo 

参数说明:

服务器地址如:QQ442518843

用户名:sa

密码:Admin123456 

数据库名:SessionDemo 

命令执行后会创建两张表:ASPStateTempApplications ,ASPStateTempSessions  和相关的存储过程

命令执行后就会成功建立起用于存储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)

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'sReleaseItemExclusive 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变量的更多相关文章

  1. 采用Opserver来监控你的ASP.NET项目系列(二、监控SQL Server与Asp.Net项目)

    前言 之前有过2篇关于如何监控ASP.NET core项目的文章,有兴趣的也可以看看. 今天我们主要来介绍一下,如何使用Opserver监控我们的SQL Server 和ASP.NET项目的异常监控 ...

  2. SQL SERVER存储引擎——04.数据

    4. SQL SERVER存储引擎之数据篇 (4.1)文件 (0)主数据文件.mdf初始文件大小至少为3MB,次要数据文件.ndf初始大小,同日志文件一样至少为512KB: (1)SQL SERVER ...

  3. SQL Server存储ntext截断问题

    SQL Server存储ntext截断问题   最近遇到一个问题:将大文本存储到数据库的时候,查询出来的文本却被截断了. 最后百度发现,作者提出 sql server management studi ...

  4. SQL Server 存储中间结果集

    在SQL Server中执行查询时,有一些操作会产生中间结果集,例如:排序操作,Hash Join和Hash Aggregate操作产生的Hash Table,游标等,SQL Server查询优化器使 ...

  5. [转]SQL Server中临时表与表变量的区别

    [转]http://blog.csdn.net/skyremember/archive/2009/03/05/3960687.aspx 我们在数据库中使用表的时候,经常会遇到两种使用表的方法,分别就是 ...

  6. SQL Server中临时表与表变量的区别

    我们在数据库中使用表的时候,经常会遇到两种使用表的方法,分别就是使用临时表及表变量.在实际使用的时候,我们如何灵活的在存储过程中运用它们,虽然它们实现的功能基本上是一样的,可如何在一个存储过程中有时候 ...

  7. SQL Server 存储字符数较大字段的问题

    SQL Server 2000专门提供了处理text,ntext,image字段的函数,他们是: TEXTPTR TEXTVALID READTEXT UPDATETEXT WRITETEXT 一般作 ...

  8. SQL Server 存储(1/8):理解数据页结构

    我们都很清楚SQL Server用8KB 的页来存储数据,并且在SQL Server里磁盘 I/O 操作在页级执行.也就是说,SQL Server 读取或写入所有数据页.页有不同的类型,像数据页,GA ...

  9. SQL Server 存储(3/8):理解GAM和SGAM页

    我们知道SQL Server在8K 的页里存储数据.分区就是物理上连续的8个页.当我们创建一个数据库,数据文件会被逻辑分为页和区,当用户对象创建时,页会分配给它用来存储数据.GAM(Global Al ...

随机推荐

  1. hdu 2071

    Ps:输出n个数里最大的 #include "stdio.h" int main(){ ],max; int i,j,n,t; while(~scanf("%d" ...

  2. [ASP.net教程]ASP.NET保存信息总结(Application、Session、Cookie、ViewState和Cache等)

    以下是关于ASP.NET中保存各种信息的对象的比较,理解这些对象的原理,对制作完善的程序来说是相当有必要的(摘至互联网,并非原创--xukunping)在ASP.NET中,有很多种保存信息的对象.例如 ...

  3. Java三大主流开源工作流引擎技术分析

    首先,这个评论是我从网上,书中,搜索和整理出来的,也许有技术点上的错误点,也许理解没那么深入.但是我是秉着学习的态度加以评论,学习,希望对大家有用,进入正题! 三大主流工作流引擎:Shark,oswo ...

  4. 算法导论----VLSI芯片测试; n个手机中过半是好的,找出哪些是好手机

    对于分治(Divide and Conquer)的题目,最重要是 1.如何将原问题分解为若干个子问题, 2.子问题中是所有的都需要求解,还是选择一部分子问题即可. 还有一点其实非常关键,但是往往会被忽 ...

  5. 程序员是怎么炼成的---OC题集--练习答案与题目(1)

    一. 1. 定义3个类,全部代码.效果:能默写,关键词全部正确. 2. ⾯面向对象和⾯面向过程有什么区别? 答:面向对象以事物(对象)为核⼼,完成事件只是一个任务.面向过程以事件为核心,为了完成任务, ...

  6. The Triangle_DP

    ime Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45620   Accepted: 27612 Description 73 8 ...

  7. 【转】使用Xcode 6将你的项目本地化

    原文转自:http://www.cocoachina.com/ios/20141004/9827.html iOS和OSX支持40种语言的本地化,Xcode无疑为这一过程提供了强有力的支持.苹果将这一 ...

  8. WCF中DataContractAttribute 类

    一.这个类的作用 使用提供的数据协定,将类型实例序列化和反序列化为 XML 流或文档.无法继承此类,(DataContractSerializer 用于序列化和反序列化在 Windows Commun ...

  9. Magento后台Grid删除Add New按钮

    开发过包含后台Grid及表等Magento完整模块的朋友应该知道,默认的,在Magento后台Grid右上方都会包含一个Add New按钮,用来添加新的item.但有些情况我们也可能不需要这个Add ...

  10. 'dict' object has no attribute 'a'

    a = {} #a.a = 'a' #AttributeError: 'dict' object has no attribute 'a' #a['a'] #KeyError: 'a' a['a'] ...