对Spring声明式事务的理解

1:当我们采用Annotation的形式声明事务时,我们会遇到对Propagation的设置,期中Propagation的取值可以为:

 


2:对REQUIRED的理解

 

   When the propagation setting is PROPAGATION_REQUIRED, alogical transaction scope is created for each method that it gets applied to. Eachsuch logical transaction scope can individually decide on rollback-only status, with an outertransaction scope being logically independent from the inner transaction scope. Of course, in case ofstandard PROPAGATION_REQUIRED behavior, they will be mapped to the same physicaltransaction. So a rollback-only marker set in the inner transaction scope does affect the outertransactions chance to actually commit (as you would expect it to).

  However, in the case where an inner transaction scopes sets the rollback-only marker, the outertransaction itself has not decided on the rollback itself, and so the rollback (silently triggered bythe inner transaction scope) is unexpected: a correspondingUnexpectedRollbackException will be thrown at that point. This isexpected behavior so that the caller of a transaction can never be misled to assumethat a commit was performed when it really was not. So if an inner transaction (that the outer caller isnot aware of) silently marks a transaction as rollback-only, the outer caller would still innocentlycall commit - and needs to receive an UnexpectedRollbackException to indicateclearly that a rollback was performed instead.

  即是需要的,当别的地方调用该方法时,当存在一个Transaction的时候,就使用原来的Transaction,当不存在的时候,就新建一个Transaction

  用法:

 


3:RequiresNew

4:Nested

 Nested

PROPAGATION_NESTED is different again in that it uses asingle physical transaction with multiple savepoints that it can roll back to.Such partial rollbacks allow an inner transaction scope to trigger a rollbackfor its scope, with the outer transaction being able to continue the physicaltransaction despite some operations having been rolled back. This is typically mapped onto JDBCsavepoints, so will only work with JDBC resource transactions (see Spring'sDataSourceTransactionManager).

    Nested是使用保存点,当内嵌的事务执行完时,外面的事务会回滚到savepoints处


5:总结


6:关于Transactional的配置说明



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波哥的技术积累

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值