c# – TransactionScope在处置之前中止了交易

前端之家收集整理的这篇文章主要介绍了c# – TransactionScope在处置之前中止了交易前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当使用TransactionScope时,它表示如果内部执行的代码回滚事务,那么父事务也将回滚.这对我有好处.但是当处置该范围时,它会抛出异常,这意味着事务已经回滚并且已中止.
那么处理这个并正确处理范围的正确方法是什么?
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
                    using (var conn = GetConnection())
                    {
                            string query = 
              @"some query that may contain transaction itself 
              or some SP whith transaction included"

                            using (var command = new sqlCommand(query,conn))
                                command.ExecuteNonQuery();
                        }
                    }
                    scope.Complete();
    } // Exception here

解决方法

即使调用了scope.Complete(),scope.Dispose()也可能抛出TransactionAborted异常.例如,一些足够聪明的存储过程可以处理异常并使用T-sql TRY / CATCH构造在T-sql脚本中中止事务,而不会向调用者抛出异常.
所以我认为我建议的最安全的方法如下:
try
{
    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
        try
        {
            using (var conn = GetConnection())
            {
                string query = 
                @"some query that may contain transaction itself 
                or some SP whith transaction included"

                using (var command = new sqlCommand(query,conn))
                command.ExecuteNonQuery();
            }
        }
        catch (sqlException ex)
        {
            // log sql Exception,if any
            throw;  // re-throw exception
        }

        scope.Complete();
    }
}
catch (TransactionAbortedException ex)
{
    // we can get here even if scope.Complete() was called.
    // log TransactionAborted exception if necessary
}

并且不用担心处理TransactionScope. scope.Dispose在抛出TransactionAborted异常之前执行清理它所需的任何操作.

猜你在找的C#相关文章