java中事务的属性_java – 春天的PROPAGATION_REQUIRED事务属性?

“需要传播”定义为

Support a current transaction,create a new one if none exists.

在上面的例子中,deleteAllCountries方法在事务中执行并提交.调用initializeCountries时没有当前事务,因此它在第二个事务中执行,并且回滚它对第一个方法所做的更改没有影响.

传播适用于嵌套方法调用,而不适用于连续调用.如果你看一下the documentation:

When the propagation setting is PROPAGATION_required,a logical

transaction scope is created for each method upon which the setting is

applied. Each such logical transaction scope can determine

rollback-only status individually,with an outer transaction scope

being logically independent from the inner transaction scope. Of

course,in case of standard PROPAGATION_required behavior,all these

scopes will be mapped to the same physical transaction. So a

rollback-only marker set in the inner transaction scope does affect

the outer transaction’s chance to actually commit (as you would expect

it to).

However,in the case where an inner transaction scope sets the

rollback-only marker,the outer transaction has not decided on the

rollback itself,and so the rollback (silently triggered by the inner

transaction scope) is unexpected. A corresponding

UnexpectedRollbackException is thrown at that point. This is expected

behavior so that the caller of a transaction can never be misled to

assume that a commit was performed when it really was not. So if an

inner transaction (of which the outer caller is not aware) silently

marks a transaction as rollback-only,the outer caller still calls

commit. The outer caller needs to receive an

UnexpectedRollbackException to indicate clearly that a rollback was

performed instead.

那么你可以看到所有这些都是关于内在和外在的 – 没有一个提到连续的呼叫.在您的情况下,对deleteAllCountries的调用是最外层的事务方法,因此当它成功完成时,Spring立即提交事务.然后你对initializeCountries的调用必须在一个单独的事务中执行,它是最外层的方法.

您的假设似乎是Spring将在第一个方法完成后保持事务处于打开状态,但这不是它的工作原理.为了获得您想要的效果,您可以在testModel上创建另一个方法,该方法包含对deleteAllCountries和initializeCountries的调用,使该方法具有事务性并为其提供属性PROPAGATION_required.这样,第二种方法的回滚将导致第一种方法的更改也被回滚,因为包装方法将它们组合在一起.否则什么都没有告诉Spring这些东西应该是同一个交易的一部分.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值