TransactionScope中的异常不明确



我在代码中使用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.Timeout,我厌倦了很多选项,但由于某种原因,我遇到了这个异常。异常发生在花费大量时间的第一次执行之后。例如,如果我有100个存储过程和文本命令,并且所有命令的时间都不到5分钟,而只有一个命令的时间超过10分钟,那么在长命令之后执行下一个命令将导致异常发生。

有人知道原因吗?

谢谢你的帮助!

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

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

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

你可以在这个答案中阅读更多关于它的信息:https://stackoverflow.com/a/10017056/205023或在此博客文章中:http://thecodesaysitall.blogspot.se/2012/04/long-running-systemtransactions.html

听起来事务只是超时了,因为它在一段时间后失败了。

最新更新