在SQL中什么是默认的最大事务超时

时间:2011-05-06 18:21:44

标签: .net sql sql-server transactions timeout

如果machine.config上没有“system.transactions”元素,那么maxTimeout的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);
}

2 个答案:

答案 0 :(得分:15)

默认= 10分钟。最大= Infinity

答案 1 :(得分:7)

没有服务器端超时是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秒。