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