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的配置说明