如果外部事务范围未完成,内部事务范围是否会回滚?
我有两个事务范围,一个在另一个范围内。我很想知道内部事务范围在提交后是否会回滚并且外部事务范围未完成。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您启动嵌套事务范围的范围选项。
如果您使用默认选项
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 calledComplete
.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.由于它们是嵌套的,内部事务将会回滚。
这不是故事的全部,取决于您如何创建嵌套事务,但默认情况下,它将回滚。
本文深入介绍了
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.
是的,你可以参考下面的代码。如果内部事务抛出错误,以下代码将回滚外部事务范围,反之亦然。
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.