2013-05-22 35 views
10

我有下面的代碼,其執行提交的單個行的數據庫表(SQL 2008/.NET 4)殭屍檢查異常 - 此SqlTransaction已完成;它不再可用 - 期間簡單提交

using (var db = new MyDbDataContext(_dbConnectionString)) 
{ 
    Action action = new Action(); 
    db.Actions.InsertOnSubmit(dbAction); 
    db.SubmitChanges(); 
} 

通常一切都很好,但曾經在一段時間,我得到以下異常:

System.InvalidOperationException: This SqlTransaction has completed; it is no longer usable. 
at System.Data.SqlClient.SqlTransaction.ZombieCheck() 
at System.Data.SqlClient.SqlTransaction.Rollback() 
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) 

上有SO,但我閱讀他們後,我不能工作的原因的一些類似的問題。

這是否僅僅是由於SQL超時(在調用完成後接近25s發生異常)?或者我應該期望在這種情況下SQL超時異常?

有誰知道還有什麼可能導致這種情況?

+0

即使你得到的異常評論,是實體保存在數據庫? –

回答

7

DataContext.SubmitChanges方法具有在它的主體下面的代碼行:

// ... 
try 
{ 
    if (this.provider.Connection.State == ConnectionState.Open) 
    { 
     this.provider.ClearConnection(); 
    } 
    if (this.provider.Connection.State == ConnectionState.Closed) 
    { 
     this.provider.Connection.Open(); 
     flag = true; 
    } 
    dbTransaction = this.provider.Connection.BeginTransaction(IsolationLevel.ReadCommitted); 
    this.provider.Transaction = dbTransaction; 
    new ChangeProcessor(this.services, this).SubmitChanges(failureMode); 
    this.AcceptChanges(); 
    this.provider.ClearConnection(); 
    dbTransaction.Commit(); 
} 
catch 
{ 
    if (dbTransaction != null) 
    { 
     dbTransaction.Rollback(); 
    } 
    throw; 
} 
// ... 

當連接超時,則catch塊被執行並且dbTransaction.Rollback();線將拋出InvalidOperationException

如果你有過代碼控制,你能趕上這樣的例外:

catch 
{ 
    // Attempt to roll back the transaction. 
    try 
    { 
     if (dbTransaction != null) 
     { 
      dbTransaction.Rollback(); 
     } 
    } 
    catch (Exception ex2) 
    { 
     // This catch block will handle any errors that may have occurred 
     // on the server that would cause the rollback to fail, such as 
     // a closed connection. 
     Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); 
     Console.WriteLine(" Message: {0}", ex2.Message); 
    } 
    throw; 
} 
1

YES!我遇到過同樣的問題。可怕的回答是,SQLServer有時在服務器端回滾事務時遇到錯誤,並且不會將錯誤傳遞迴客戶端。哎呀!

請查看Google Group microsoft.public.dotnet.framework.adonet "SqlTransaction.ZombieCheck error" Colberd Zhou [MSFT]很好地解釋了這一點。

,看到aef123對this SO post