Dot-Net

在 SQL 中,預設的最大事務超時是多少

  • February 27, 2013

maxTimeout如果 machine.config 上沒有“system.transactions”元素,則machine.config 中的預設值是什麼(參見範例)?

<system.transactions>
  <machineSettings maxTimeout="??:??:??" />
</system.transactions>

我之所以問這個問題是因為程式碼由於以下異常而崩潰,並且似乎與事務超過超時有關,它在SaveChanges方法期間崩潰並且我收到的異常如下:

The transaction associated with the current connection has completed
but has not been disposed. The transaction must be disposed before
the connection can be used to execute SQL statements.

這是崩潰的一段程式碼:

using (TransactionScope transaction = TransactionHelper.CreateTransactionScope())
{
   using (EFContext efDBContext = new EFContext())
   {
       try
       {
           efDBContext.Connection.Open();  

           foreach (MyEntity currentEntity in myEntities)
           {
               //Insertion
           }

           transaction.Complete();
       }
       catch (Exception ex)
       {
           //Inspect current entity
           //Get Total Time of the run
           //Number of entities processed
       }
       finally
       {
           if (esfDBContext.Connection.State == ConnectionState.Open)
               esfDBContext.Connection.Close();
       }
   }
}

這就是我創建的方式TransactionScope

public static TransactionScope CreateTransactionScope(TransactionScopeOption option = TransactionScopeOption.Required, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
{
   var transactionOptions = new TransactionOptions()
   {
       Timeout = TimeSpan.MaxValue,
       IsolationLevel = isolationLevel
   };

   return new TransactionScope(option, transactionOptions);
}

預設 = 10 分鐘。最大 =無窮大

沒有伺服器端超時是 MS SQL。

在設定的持續時間後,總是由客戶端引發異常。

http://blogs.msdn.com/b/khen1234/archive/2005/10/20/483015.aspx

您真的很想查看命令超時。

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx

這裡的預設值為 30 秒。

引用自:https://stackoverflow.com/questions/5915462