嵌套/儿童的TransactionScope回滚 [英] Nested/Child TransactionScope Rollback

查看:151
本文介绍了嵌套/儿童的TransactionScope回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想窝TransactionScopes(.NET 4.0),你会在SQL Server的嵌套事务,但它看起来像他们操作不同。我希望我的孩子的交易,以便能够如果他们不回退,但允许父事务,以决定是否提交/回滚整个操作。问题是,当第一个完成时,事务回滚。我认识到,完全是不同的承诺。

I am trying to nest TransactionScopes (.net 4.0) as you would nest Transactions in SQL Server, however it looks like they operate differently. I want my child transactions to be able to rollback if they fail, but allow the parent transaction to decide whether to commit/rollback the whole operation. The problem is when the first complete occurs, the transaction is rolled back. I realize that complete is different to commit.

什么,我试图做一个大大简化的例子:

A greatly simplified example of what I am trying to do:

static void Main(string[] args)
{
    using(var scope = new TransactionScope()) // Trn A
    {
        // Insert Data A

        DoWork(true);
        DoWork(false);

        // Rollback or Commit
    }
}

// This class is a few layers down
static void DoWork(bool fail)
{
    using(var scope = new TransactionScope()) // Trn B
    {
        // Update Data A

        if(!fail)
        {
            scope.Complete();
        }
    }
}

我不能使用燮preSS或RequiresNew选项Trn键乙依赖于由Trn键A.插入数据。如果我使用这些选项,Trn键B被阻止Trn键一个。

I can't use the Suppress or RequiresNew options as Trn B relies on data inserted by Trn A. If I do use those options, Trn B is blocked by Trn A.

任何想法如何,我会得到它的工作,或者如果它甚至有可能使用System.Transactions的命名空间?

Any ideas how I would get it to work, or if it is even possible using the System.Transactions namespace?

感谢

推荐答案

你可能不会喜欢这个答案,但是...

You're probably not going to like this answer, but...

投票内嵌套的范围

虽然嵌套范围内可以加入的根范围内的环境事务,调用的完成在嵌套范围已经没有根的范围影响。只有从根范围内所有范围到最后嵌套的作用域投提交事务,将事务被提交。

Although a nested scope can join the ambient transaction of the root scope, calling Complete in the nested scope has no affect on the root scope. Only if all the scopes from the root scope down to the last nested scope vote to commit the transaction, will the transaction be committed.

(从实施的隐性交易使用事务范围

的TransactionScope 类遗憾的是并没有提供任何机制(据我所知)的分离工作单位。这是全有或全无。您可以prevent的任意的交易从使用发生的具体工作单元 TransactionScopeOption.Sup preSS ,但是这可能是不是你想要的,因为你会失去原子不管什么是范围内。

The TransactionScope class unfortunately doesn't provide any mechanism (that I know of) for segregating units of work. It's all or nothing. You can prevent any transaction from occurring on a specific unit of work by using TransactionScopeOption.Suppress, but that is probably not what you want, as you would then lose atomicity for whatever is inside that scope.

当你使用的TransactionScope 只有一种环境交易。一旦的TransactionScope 被处置,或者被收集无完成正在执行,整个环境事务被回滚;就是这样,比赛结束了。

There is only one "ambient" transaction when you use a TransactionScope. Once a TransactionScope is disposed or gets collected without Complete being executed, the whole ambient transaction gets rolled back; that's it, game over.

事实上,SQL Server不支持真正的嵌套事务可言,但它的的可能(虽然有点不直观),以实现与适当使用的相同的最终结果SAVE TRAN 语句。重新实现这个逻辑作为存储过程(或其中几个),如果你需要这种特殊的行为可能是你最好的选择。

In fact, SQL Server doesn't support true nested transactions at all, although it is possible (though somewhat unintuitive) to achieve the same end result with appropriate use of SAVE TRAN statements. Re-implementing this logic as a Stored Procedure (or several of them) might be your best option if you require this particular behaviour.

这篇关于嵌套/儿童的TransactionScope回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆