2014-10-12 72 views
4

我有一个ASP.NET Web窗体应用程序,使用.Net连接器为MySql实体框架。ASP.NET Web窗体和MySql实体框架:“不支持嵌套事务”

我在过去使用MySql制作了一个Web应用程序,并且从未遇到过这个问题。

我目前每天获得约20000页面浏览量,每天大约有10次'嵌套事务不支持'的错误。

我无法弄清楚它为什么会发生,为什么它只发生一次。

母版页预压事件中,我得到了用户的整数ID,并将其保存到公共变量:

using (var entity = new Entities()) 
       { 
        var user = entity.my_aspnet_users.Single(i => i.name == Context.User.Identity.Name); 
        UserID = user.id; 
        if (HttpContext.Current.Request.Url.AbsolutePath != "###" && HttpContext.Current.Request.Url.AbsolutePath != "###") 
        { 
         if (user.Player == null) 
          Response.Redirect("###", true); 
         else 
          PlayerID = user.Player.PlayerID; 
        } 
       } 

然后在页面上:

using (var entity = new Entities()) 
          if (entity.Players.Count(i => ###) == 1) 
          { 
           var user = entity.my_aspnet_users.Single(i => i.id == Master.UserID); 
           if (user.Player == null) 
           { 
            user.Player = entity.Players.Single(i => ###); 
            entity.SaveChanges(); 
           } 
           Response.Redirect("###", false); 
          } 

错误被抛出在SaveChanges()上。我不知道为什么它只是偶尔发生。当用户在网站上注册时,我也有时会得到同样的错误,尽管这一切似乎都是随机的和罕见的。

无论如何,其中之一,我认为在MySql中允许嵌套事务?有没有需要设置的设置?我的MySql实例是5.6.17,我相信使用默认参数 - 在Amazon RDS中设置。

'User.Identity.Name'访问数据库吗?你有什么想法,我可以看看解决这个问题吗?

这里的,如果有帮助的一些异常的堆栈跟踪:

**Exception on example code:** 
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Data.Entity.Core.EntityException: An error occurred while starting a transaction on the provider connection. See the inner exception for details. ---> System.InvalidOperationException: Nested transactions are not supported. 

at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception) 

at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex) 

at MySql.Data.MySqlClient.MySqlConnection.BeginTransaction(IsolationLevel iso) 

at MySql.Data.MySqlClient.MySqlConnection.BeginDbTransaction(IsolationLevel isolationLevel) 

at System.Data.Common.DbConnection.BeginTransaction(IsolationLevel isolationLevel) 

at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__0(DbConnection t, BeginTransactionInterceptionContext c) 

at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) 

at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.BeginTransaction(DbConnection connection, BeginTransactionInterceptionContext interceptionContext) 

at System.Data.Entity.Core.EntityClient.EntityConnection.<>c__DisplayClassf.b__d() 

at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation) 

at System.Data.Entity.Core.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel) 

--- End of inner exception stack trace --- 

at System.Data.Entity.Core.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel) 

at System.Data.Entity.Core.EntityClient.EntityConnection.BeginTransaction() 

at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) 

at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) 

at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.b__27() 

at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation) 

at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction) 

at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options) 

at System.Data.Entity.Internal.InternalContext.SaveChanges() 

at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() 

at System.Data.Entity.DbContext.SaveChanges() 

at ###.Account.###.ButtonSubmit###_Click(Object sender, EventArgs e) 

at System.Web.UI.WebControls.Button.OnClick(EventArgs e) 

at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 

at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 

at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 

at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

at System.Web.UI.Page.HandleError(Exception e) 

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

at System.Web.UI.Page.ProcessRequest() 

at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) 

at System.Web.UI.Page.ProcessRequest(HttpContext context) 

at ASP.account_###.ProcessRequest(HttpContext context) 

at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 

at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 



**Exception on creating a user:** 
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: Nested transactions are not supported. 

at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception) 

at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex) 

at MySql.Data.MySqlClient.MySqlConnection.BeginTransaction(IsolationLevel iso) 

at MySql.Data.MySqlClient.MySqlConnection.BeginTransaction() 

at MySql.Web.Security.MySQLMembershipProvider.GetUser(Object providerUserKey, Boolean userIsOnline) 

at MySql.Web.Security.MySQLMembershipProvider.GetUser(String username, Boolean userIsOnline) 

at MySql.Web.Security.MySQLMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, MembershipCreateStatus& status) 

at System.Web.UI.WebControls.CreateUserWizard.AttemptCreateUser() 

at System.Web.UI.WebControls.CreateUserWizard.OnNextButtonClick(WizardNavigationEventArgs e) 

at System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e) 

at System.Web.UI.WebControls.CreateUserWizard.OnBubbleEvent(Object source, EventArgs e) 

at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) 

at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) 

at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 

at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 

at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 

at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

at System.Web.UI.Page.HandleError(Exception e) 

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

at System.Web.UI.Page.ProcessRequest() 

at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) 

at System.Web.UI.Page.ProcessRequest(HttpContext context) 

at ASP.register_aspx.ProcessRequest(HttpContext context) 

at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 

at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

回答

6

你有可能碰上this bug in MySQL Connector/NET

是什么导致了这个bug:

  1. 守则要求查询
  2. 事务1查询下开始执行
  3. 查询的执行,并导致一个错误在MySQL
  4. 事务1不回滚
  5. 代码调用执行查询B
  6. 查询B的事务2开始
  7. MySQL连接/ NET抛出异常

bug是点4:事务1保持打开错误后(或至少所述连接器是仍然相信它的左开)。由于连接池,调用查询A和查询B的代码可以完全不相关。另外,如果点4和点5之间的时间足够长,则交易回滚,因此具有罕见性和随机性。

不幸的是,MySQL尚未修复。我知道的唯一解决方法是下载Connector/NET的源代码并自行修复/构建它。

+0

感谢您的信息,我认为你是对的 我结束了这样做;我的大部分问题都是使用MySql成员资格提供程序,因此我使用了它们的源代码,并提出了一些我不需要的疑问。我仍然有这个问题,但现在不太频繁,所以我没有担心 – barrett777 2015-03-26 00:56:09