我的 asp.net 应用程序每天都运行良好。 直到上个月,我的网站开始出现 2-3 次与 Sqlsession 状态服务器有关的问题 关注:
Blockquote System.Web.HttpException Exception of type 'System.Web.HttpException' was thrown. at System.Web.HttpAsyncResult.End() at System.Web.SessionState.SessionStateModule.EndAcquireState(IAsyncResult ar) at System.Web.HttpApplication.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) ================================================== Exception: System.Web.HttpException Unable to connect to SQL Server session database. at System.Web.SessionState.SqlSessionStateStore.ThrowSqlConnectionException(SqlConnection conn, Exception e) at System.Web.SessionState.SqlSessionStateStore.SqlStateConnection..ctor(SqlPartitionInfo sqlPartitionInfo) at System.Web.SessionState.SqlSessionStateStore.GetConnection(String id, Boolean& usePooling) at System.Web.SessionState.SqlSessionStateStore.DoGet(HttpContext context, String id, Boolean getExclusive, Boolean& locked, TimeSpan& lockAge, Object& lockId, SessionStateActions& actionFlags) at System.Web.SessionState.SqlSessionStateStore.GetItemExclusive(HttpContext context, String id, Boolean& locked, TimeSpan& lockAge, Object& lockId, SessionStateActions& actionFlags) at System.Web.SessionState.SessionStateModule.GetSessionStateItem() at System.Web.SessionState.SessionStateModule.PollLockedSessionCallback(Object state) ================================================== Exception: System.InvalidOperationException Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached. at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at System.Web.SessionState.SqlSessionStateStore.SqlStateConnection..ctor(SqlPartitionInfo sqlPartitionInfo)
然后我开始查看我的 session 数据库服务器并在我的 sql 客户端中执行“exec sp_who”,结果发现了很多 AspState 操作的记录。
我不知道是什么导致了这种问题,因为我们没有改变任何严重的事情 在我们的应用程序中,仅修复了一些错误。
这是我的网络应用程序环境的详细信息:
asp.net 3.5(从 1.1 转换)... 在 2 个服务器群, session 状态为 sqlmode。
有没有人知道这个问题或对调查有任何想法? 谢谢
最佳答案
这篇文章首次提出已经有一段时间了,但我最近在生产环境中遇到了这个确切的问题,并认为它可能对将来的其他人有用。
我们有一个 ASP.NET MVC Web 应用程序在 .NET 4.5.2 上运行,在两个节点之间进行负载平衡。该应用程序配置为将 session 存储在 SQL Server 2012(版本 11.0.5058.0)上的 ASPState 数据库中。我们正遭受间歇性超时的困扰:
Exception information: Exception type: HttpException Exception message: Unable to connect to SQL Server session database. Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
我认为这里关于确保代码库中没有其他连接泄漏的一些答案是误导性的。为每个不同的连接字符串创建一个连接池,因此对与其他数据库的连接的任何修复都不会对 session 状态数据库产生有益的影响。
我们通过两个更改解决了这个问题:
通过覆盖 web.config 文件中连接字符串中的默认值 100 来增加最大池大小:
sqlConnectionString="data source=SERVERNAME;Initial Catalog=AspState;user id=AspStateUser;password=xxxxx;App=xxxx; Max Pool Size=200;"
很明显,AspState 数据库是使用 .NET 2.0 命令行创建的,重要的是,dbo.DeleteExpiredSessions SP 已知存在阻塞问题。使问题更加复杂的是,该作业被配置为每分钟执行一次该过程。 AspState 数据库是使用 .NET 4.0 命令行重新创建的,如下所示:
C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regsql.exe -ssadd - sstype c -S OURSERVERNAME -d "AspState" -E
重要的是,更高版本包括 DeleteExpiredSessions 过程的性能改进版本,它包含一个游标,可以一次删除一个过期的 session 。我们还更改了关联作业的计划,使其每小时执行一次,而不是每分钟执行一次。
关于c# - 无法连接到 SQL Server session 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/435582/