如果外部事务范围未完成,内部事务范围是否会回滚?

发布于 2024-10-08 14:22:24 字数 58 浏览 2 评论 0原文

我有两个事务范围,一个在另一个范围内。我很想知道内部事务范围在提交后是否会回滚并且外部事务范围未完成。

I have two transaction scopes, one within another. I would love to know if the inner transaction scope will be rolled back after it has been committed and the outer one does not complete.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂归处 2024-10-15 14:22:24

这取决于您启动嵌套事务范围的范围选项。

如果您使用默认选项 TransactionScopeOption.Required ,则嵌套作用域将与外部作用域登记在同一事务中,因此当外部作用域回滚时,内部作用域也会回滚,即使它已调用完成

但是,如果您使用TransactionScopeOption.RequiresNew,则嵌套作用域将开始自己的事务并与外部作用域分开完成它,因此即使外部作用域回滚,它也不会回滚。

如果您使用TransactionScopeOption.Suppress,则嵌套作用域将不会参与外部事务,并将以非事务方式完成,因此不会构成外部事务回滚时将回滚的工作的一部分后退。

It depends on the scope option you start the nested transaction scope with.

If you use the default option TransactionScopeOption.Required then the nested scope will enlist in the same transaction as the outer scope and as such when the outer scope rolls back the inner scope will also be rolled back even if it has called Complete.

If, however, you use TransactionScopeOption.RequiresNew then the nested scope will begin its own transaction and complete it separately from the outer scope, so it will not roll back even if the outer scope rolls back.

If you use TransactionScopeOption.Suppress then the nested scope will not take part in the outer transaction and will complete non-transactionally, thus does not form part of the work that would be rolled back if the outer transaction rolls back.

梦魇绽荼蘼 2024-10-15 14:22:24

由于它们是嵌套的,内部事务将会回滚。

这不是故事的全部,取决于您如何创建嵌套事务,但默认情况下,它将回滚。

本文深入介绍了 TransactionScope 和应该可以回答你的大部分问题。


是否分发并不重要。

Since they are nested, the inner transaction will roll back.

This is not the whole story, and depends on how you create the nested transaction, but by default, it will roll back.

This article goes into depth about TransactionScope and should answer most of your questions.


Being distributed or not is irrelevant.

风筝有风,海豚有海 2024-10-15 14:22:24

是的,你可以参考下面的代码。如果内部事务抛出错误,以下代码将回滚外部事务范围,反之亦然。

   public bool rootMethod(){
          using (var transaction = new(TransactionScopeOption.RequiresNew))
           try{
           // your code here
           SomeController someController = new SomeController();
           var responseFromChildMethod = someController.childMethodWithTxn();

       // your logic here

       transaction.Complete();
       return true;
       }
       catch(Exception ex){
       transaction.Dispose();
       return false;
       }
    } 
}

SomeController.cs

public bool childMethodWithTxn(){
  using(var newTransaction =  new TransactionScope()){
    try{
      //your code here
      newTransaction.Complete();
      return true;
    }
    catch(Exception ex){
           newTransaction.Dispose();
           return false;
           }

  }
}

Yes it will, you can refer to code below. Following code wil roll back outer transaction scope if inner transaction throw error and vice versa.

   public bool rootMethod(){
          using (var transaction = new(TransactionScopeOption.RequiresNew))
           try{
           // your code here
           SomeController someController = new SomeController();
           var responseFromChildMethod = someController.childMethodWithTxn();

       // your logic here

       transaction.Complete();
       return true;
       }
       catch(Exception ex){
       transaction.Dispose();
       return false;
       }
    } 
}

SomeController.cs

public bool childMethodWithTxn(){
  using(var newTransaction =  new TransactionScope()){
    try{
      //your code here
      newTransaction.Complete();
      return true;
    }
    catch(Exception ex){
           newTransaction.Dispose();
           return false;
           }

  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文