![](/img/trans.png)
[英]Why is failed transaction not rolling back with TransactionScope?
[英]I am receiving “PROMOTE TRANSACTION request failed because there is no local transaction active” in a System.Transaction.TransactionScope
我有一個運行了一周左右的asp.net MVC應用程序,然后今天開始出現此錯誤。
在.net 4.0上運行,Win 2K8 R2與Win 2K8 R2上的SQL Server 2K8進行通信。
所有服務器上都運行着MS DTC。
我正在做一個簡單的aspnet成員資格創建,然后在我自己的用戶表(在范圍內)中創建其他數據。
好像一個人遇到錯誤后,每個人都會得到嘗試在此之后創建帳戶的錯誤。 數據庫的其他功能正在運行。
這是堆棧跟蹤:
System.Transactions.TransactionAbortedException: The transaction has aborted. ---> System.Transactions.TransactionPromotionException: Failure while attempting to promote transaction. ---> System.Data.SqlClient.SqlException: The PROMOTE TRANSACTION request failed because there is no local transaction active.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
...
at System.Data.SqlClient.SqlConnection.Open()
at Elmah.SqlErrorLog.Log(Error error) in c:\builds\ELMAH\src\Elmah\SqlErrorLog.cs:line 158
at KeyesTechnology.DemandLaunch.BusinessEntities.LoginAccount.CreateAccount() in c:\Products\DemandLaunch\Staging\KeyesTechnology\DemandLaunch\BusinessEntities \LoginAccount.cs:line 141
at www.ViewModels.CreateAccountViewModel.Process() in c:\Products\DemandLaunch\Staging\mvc\www\ViewModels\CreateAccountViewModel.cs:line 89
at www.Controllers.AccountController.Create(CreateAccountViewModel model, String returnUrl) in c:\Products\DemandLaunch\Staging\mvc\www\Controllers\AccountController.cs:line 85
at lambda_method(Closure , ControllerBase , Object[] )
碼:
using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
{
try
{
//Create ASP.NET Membership User
if (success)
{
//Create Domain Specific User Object
// Forms Authenication call
}
else
{
throw Exception();
}
//Send Welcome Email
//Subscribe to Email List
scope.Complete();
}
catch(Exception exc)
{
Elmah.Error error = new Elmah.Error(exc, HttpContext.Current);
Elmah.ErrorLog log = Elmah.ErrorLog.GetDefault(HttpContext.Current);
log.Log(error); //Error throws on this line
result = false;
}
}
我發現我最后一次寫到基於SQL的ELMAH
並且它是在作用域完成之后,因此它將永遠無法工作。 我已經修復了該錯誤,但
它沒有告訴我為什么會發生錯誤。
為了測試,我在scope.Complete()語句之前和之后都拋出了異常
我在scope.Complete()之前犯了一個故意的錯誤。 我在頁面中沒有錯誤,也沒有記錄任何ELMAH錯誤。
我在scope.Complete()之后立即犯了一個故意錯誤。 我在最終捕獲中得到范圍已經完成的錯誤。
我無法弄清楚什么會促使PROMOTE嘗試發生,以及為什么會發生。
該網站運行了10天很好,然后我們收到此錯誤,隨后所有人嘗試創建一個帳戶都收到了此錯誤。 盡管數據庫的其他部分工作正常。 在我們重新啟動SQL Server之前,只是弄亂了此功能。
嘗試為您的記錄電話執行此操作:
using(TransactionScope scope
= new TransactionScope(TransactionScopeOption.Suppress))
{
log.Log(error); //Error throws on this line
}
我認為您的日志記錄調用正在嘗試提升為分布式事務(當您嘗試單擊錯誤記錄器時)。 此代碼將從事務中刪除日志記錄調用。 在進行分布式工作時,記錄器遇到了相同的問題。 我將收到一個錯誤,失敗,然后登錄,然后退出范圍並回滾所有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.