【问题标题】:In SQL What is the default max transaction timeout在 SQL 中,默认的最大事务超时是多少
【发布时间】:2011-08-20 09:41:04
【问题描述】:

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

【问题讨论】:

  • 几乎没有人知道这一点,但您需要非常小心。您的 machine.config 始终优先:blog.muonlab.com/2011/08/03/… - “...如果您提供大于 machine.config 值的超时,[运行时] 将忽略它并使用 machine.config 之一。静默。没有抱怨,没有警告。”
  • 创建具有最大超时时间的TransactionScope 时,请考虑使用TransactionManager.MaximumTimeout 而不是TimeSpan.MaxValue(参见stackoverflow.com/a/21935333/7665486

标签: .net sql sql-server transactions timeout


【解决方案1】:

默认 = 10 分钟。最大 = Infinity

【解决方案2】:

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

【讨论】:

  • 很抱歉我没有收到您的评论,事务持续时间在服务器中的几个地方配置,“machine.config”是最重要的,“app.config”和在代码级别,如果使用代码,它将忽略 app.config,但如果超过它似乎正在使用 TransactionManager.DefaultTimeout,这似乎是 1 分钟,我不确定这一点,但我试图确认这一点。
  • 通常有 SQL 上的命令超时和 .NET 中的页面超时。实际抛出的异常是什么?
  • 与当前连接关联的事务已完成但尚未被释放。必须先释放事务,然后才能使用连接执行 SQL 语句。
  • 听起来您没有清理对象并关闭连接。你能把问题中的代码粘贴到我可以调试吗?
  • 查看上面的代码,我将所有调用都包含在 using 语句中,这应该不是问题。
猜你喜欢
  • 1970-01-01
  • 2013-12-12
  • 2010-11-23
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 2012-08-21
  • 2019-07-26
  • 2014-02-06
相关资源
最近更新 更多