交易范围不抑制

时间:2019-03-08 15:45:45

标签: c# transactions

我正在通过引发新的Exception(如果事务是否回滚)来实践TransactionScope,即使scope2具有抑制选项,scope1也不会提交。我期望两个sqlcommand块将被提交。我的代码有什么问题?

using (TransactionScope scope1 = new TransactionScope(TransactionScopeOption.Required))
        {
            using (SqlConnection conn = new SqlConnection(AppGlobalVariables.CONNECTION_STRING))
            {
              //success sqlcommand...
            }

            using (SqlConnection conn = new SqlConnection(AppGlobalVariables.CONNECTION_STRING))
            {
              //success sqlcommand... 
            }

            using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.Suppress))
            {
                //this throws new Exception(string)
                ThrowAnError("Scope 2 Error");
            }
            scope1.Complete();
        }

0 个答案:

没有答案