required: if there is a transaction, use the current transaction. If there is no transaction, create a new transaction (default)
requires_new: always create a new transaction whether there is a transaction or not
Needed: similar to required, the difference is that a restore point will be set. If there is a problem, only this method will be restored, not the external method.
Supports: supports the current transaction. If there is no transaction, it will be executed in a non transaction manner
not_supported: executed in a non transactional manner. If a transaction currently exists, it will be suspended
never: always execute under non transaction. If there is a transaction, an error will be reported
mandatory: it is always executed under a transaction. If there is no transaction, an error will be reported
------------------------------------------------------------------------------Specific examples---------------------------------------------------------------------------------------------
REQUIRED
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) void a(){ before(); b(); after(); } @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) void b(){ //crud... }
When a() calls b(): a() will start a transaction, and then b() will continue to use the transaction of a().
Calling b(): b() alone opens a new transaction. No matter which one reports an error (exception), before() b() after() will be rolled back.
If the nested execution method requires successful execution or rollback together, select the transaction propagation level.
REQUIRES_NEW
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) void a(){ before(); b(); after(); } @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) void b(){ //crud... }
When a() calls b(): a() will start a transaction, and then b() will not continue to use the transaction of a(), but will start a transaction separately. If b() reports an error, only b() will be rolled back and before() will not be affected. An error in after() will not affect b().
If the nested execution method requires independent transactions and cannot affect each other, select this transaction propagation level.
NESTED
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) void a(){ before(); b(); after(); } @Transactional(rollbackFor = Exception.class, propagation = Propagation.NESTED) void b(){ //crud... }
When a() calls b(): a() will start a transaction, and then b() will continue to use the transaction of a(). However, b() will set a restore point. When b() reports an error, it will only roll back b() itself, not before(). If after() reports an error, all three will be rolled back.
If the nested execution method requires the internal method to roll back itself only when it makes an error, and the external method fails to roll back all, and an execution is automatically started when it is executed separately, select the transaction propagation level.
MANDATORY
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) void a(){ before(); b(); after(); } @Transactional(rollbackFor = Exception.class, propagation = Propagation.MANDATORY) void b(){ //crud... }
When a() calls b(): the same as REQUIRED.
Calling b() alone: an error is reported.
If the nested execution method requires successful execution or rollback together, and non transactional execution is not allowed when executing alone, select the transaction propagation level.
NEVER
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) void a(){ before(); b(); after(); } @Transactional(rollbackFor = Exception.class, propagation = Propagation.NEVER) void b(){ //crud... }
When a() calls b(): an error is reported.
Call b() separately: do not open the transaction.
If the nested execution method requires that the internal method is not allowed to be executed in a transaction, and must be executed in a non transaction manner when executing alone, select the transaction propagation level.
SUPPORTS
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) void a(){ before(); b(); after(); } @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS) void b(){ //crud... }
When a() calls b(): the same as REQUIRED.
Call b() separately: do not open the transaction.
If the nested execution method requires successful execution together or rollback together, and is executed in a non transactional manner when executed separately, select the transaction propagation level.
NOT_SUPPORTED
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) void a(){ before(); b(); after(); } @Transactional(rollbackFor = Exception.class, propagation = Propagation.NOT_SUPPORTED) void b(){ //crud... }
When a() calls b(): suspend the transaction of a(). If not, b(), after() cannot affect b().
Call b() separately: do not open the transaction.
If the nested execution method requires that the internal nested method will not affect the external method transaction, and the internal method does not need a transaction. When it is executed separately, it will be executed in a non transactional manner, select the transaction propagation level.