【问题标题】:Unclear exception from TransactionScope来自 TransactionScope 的不明异常
【发布时间】:2011-06-19 07:14:50
【问题描述】:

我在我的代码中使用 TransactionScrope 在 SQL Server 2008 R2 上执行 50 SQL 命令。 这是我的代码示例:

   private void DoSomething()
    {
        bool IsComplete = false;
        SqlCommand sqlComm = null;
        //6 hours!!!
        TimeSpan ts1 = new TimeSpan(6, 0, 0);
        try
        {
            using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew, ts1))
            {
                using (SqlConnection sqlConn = new SqlConnection(GetConnectionString()))
                {
                    //open sql connection
                    sqlConn.Open();
                    try
                    {
                        //create new sqlCommand
                        sqlComm = new SqlCommand();
                        for (int i = 1; i <= 2; i++)
                        {
                            IsComplete = true;
                            //This command takes 15 minutes
                            sqlComm.CommandText = "exec TestSp";
                            sqlComm.Connection = sqlConn;
                            sqlComm.CommandType = CommandType.Text;
                            sqlComm.CommandTimeout = 18000;
                            //Executing my command
                            int j = sqlComm.ExecuteNonQuery();
                            sw.WriteLine("Finsh Executing SQL Command:" + DateTime.Now.ToLongTimeString());
                            sw.Flush();
                        }
                        //End
                        IsComplete = true;
                    }
                    catch (Exception ex)
                    {
                        IsComplete = false;
                        string Message = ex.Message;
                    }
                    finally
                    {
                        if (sqlComm != null)
                            sqlComm.Dispose();
                        if (IsComplete)
                            t.Complete();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            string messagee = ex.Message;
            //do something
        }
        finally
        {
            MessageBox.Show("Finsh");
        }
    }

当程序花费超过 10 分钟时,我会收到此异常:

与当前连接关联的事务已完成但尚未处理。必须先释放事务,然后才能使用连接执行 SQL 语句。

通过更改 TimeSpam 和更改 SqlCommand.Timout,我厌倦了很多选项,但由于某种原因,我得到了这个异常。该异常发生在第一次执行后需要很长时间。例如,如果我有 100 个存储过程和文本命令,并且所有命令耗时不到 5 分钟,而只有一个命令耗时超过 10 分钟,那么在长命令之后执行下一个命令将导致异常发生。

有人知道原因吗?

感谢您的帮助!

【问题讨论】:

  • 6 小时的交易实在是太疯狂了。非常糟糕的巫毒教。交易应该非常简短。我不认为这有帮助。我会重新设计这个...

标签: c# sql stored-procedures ado.net transactionscope


【解决方案1】:

我认为这里要尝试的只是将事务完成 移动到连接之外,即

using(var tran = ...)
{
    bool isComplete;
    using(var conn = ...)
    {
         //...
    }

    if(isComplete)
        tran.Complete();
}

另请注意,大多数次,让异常处理为我们绕过 Complete() 会更容易,即

using(var tran = ...)
{
    using(var conn = ...)
    {
        //...
    }

    // if we get an exception, we won't get here
    tran.Complete();
}

【讨论】:

  • 我试过了,但还是不行... 10 分钟后,下一个 sqlCommand 返回相同的异常。还有其他想法吗?
  • @user436862 我的主要 想法是,这项工作太大了,无法进行交易...... 10 分钟? 6个小时?大多数交易持续(最多)
  • 我得到了一个从 int 到 bigint 的转换表的事务。我创建空表并从通过 int 键绑定的 4 个表中复制所有记录(在新表中通过 bigint 键),因此我需要将旧表中的所有数据一一插入到新事务中。因为我有数百万条记录,所以这个交易需要超过 15 分钟...
【解决方案2】:

当事务运行的时间长于maxTimeoutSystem.Transactions 时,可能会发生此错误。 maxTimeout 的默认值为 10 分钟。

您可以在此答案中了解更多信息:https://stackoverflow.com/a/10017056/205023 或在此博客文章中:http://thecodesaysitall.blogspot.se/2012/04/long-running-systemtransactions.html

【讨论】:

    【解决方案3】:

    听起来事务只是超时了,看到它在设定的时间后失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多