小编典典

在SQL中,默认的最大事务超时时间是多少

sql

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);
}

阅读 263

收藏
2021-05-16

共1个答案

小编典典

默认值= 10分钟。最大值=无限

2021-05-16