MySQL会话状态提供程序在过期的会话上崩溃

时间:2013-08-23 07:38:10

标签: asp.net mysql iis session-state mysql-connector

我有一个使用MySQL Connector 6.7.4在IIS 7.5 ASP.NET 4中运行的站点,我正在尝试将其设置为在我的数据库中存储会话状态。 它开始工作正常(我可以存储状态)。但是当我让浏览器打开很长时间然后尝试刷新页面时,我收到以下错误:


为'PRIMARY'键重复输入'jouwlxjdufet2fyvf4cwefn2-1'

说明:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

异常详细信息:

MySql.Data.MySqlClient.MySqlException: Duplicate entry 'jouwlxjdufet2fyvf4cwefn2-1' for key 'PRIMARY'

来源错误:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

堆栈追踪:

[MySqlException (0x80004005): Duplicate entry 'jouwlxjdufet2fyvf4cwefn2-1' for key 'PRIMARY']
   MySql.Data.MySqlClient.MySqlStream.ReadPacket() +492
   MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) +450
   MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) +136
   MySql.Data.MySqlClient.MySqlDataReader.NextResult() +1121
   MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) +2648
   MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() +140
   MySql.Web.SessionState.MySqlSessionStateStore.CreateUninitializedItem(HttpContext context, String id, Int32 timeout) +553

[ProviderException: An exception occurred. Please check the event log.]
   MySql.Web.SessionState.MySqlSessionStateStore.HandleMySqlException(MySqlException e, String action) +281
   MySql.Web.SessionState.MySqlSessionStateStore.CreateUninitializedItem(HttpContext context, String id, Int32 timeout) +709
   System.Web.SessionState.SessionStateModule.GetSessionStateItem() +304
   System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData) +1076
   System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +115
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1008

我的web.config包含以下内容

<system.web>
  <sessionState mode="Custom" cookieless="false" regenerateExpiredSessionId="false" customProvider="MySqlSessionStateProvider" timeout="20">
    <providers>
      <add name="MySqlSessionStateProvider" type="MySql.Web.SessionState.MySqlSessionStateStore, MySql.Web, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" applicationName="/skygdadmin" description="" connectionStringName="MySqlServer" writeExceptionsToEventLog="False" autogenerateschema="True" enableExpireCallback="True" />
    </providers>
  </sessionState>
...

我可以在数据库中看到已经存在的条目:

表:my_aspnet_sessions

SessionId: 'jouwlxjdufet2fyvf4cwefn2'
ApplicationId: '1'
Created: '2013-08-22 08:29:34'
Expires: '2013-08-22 09:49:39'
LockDate: '2013-08-22 09:29:39'
LockId: '419'
Timeout: '20'
Locked: '0'
SessionItems: [lots of stuff]
Flats: '0'

此外,我可以看到my_aspnet_sessioncleanup表定期更新,表明会话清理正在运行(每10分钟一次)。

有没有人对从何处开始调试有任何想法? 谢谢 大卫

1 个答案:

答案 0 :(得分:2)

修正了它!

我挖掘了6.8.3版的代码。

在SessionProvider.cs文件中:通过始终使用INSERT查询,代码假设数据库中不存在会话密钥的行 - 这对于创建会话时工作正常,但如果抛出异常则抛出异常会话密钥被重用。

我用相同文件中的一些类似代码修复它,我不再得到异常。修改后的代码使用REPLACE查询而不是INSERT。

我已将更新的文件和补丁附加到MySQL站点上的相应错误(我的基本版本是6.8.3)。您可以下载6.8.3源代码并对其进行修补。如果你自己编译它,你还必须在AssemblyInfo.cs中注释掉AssemblyKeyName属性(对于强大的程序集名称)。

希望MySQL人们能够将我的小修正案推向大众流通。

请参阅我的笔记:

http://bugs.mysql.com/bug.php?id=70409

并且,直到MySql更新官方发布版的解决方法:

https://github.com/xavierjefferson/mysql-web-fixed