Spring defines several transaction propagation methods:
PROPAGATION_REQUIRED supports the current transaction. If no transaction exists, a new transaction is created. This is the most common choice.
PROPAGATION_SUPPORTS supports the current transaction. If no transaction exists, it is executed in non-transaction mode.
PROPAGATION_MANDATORY supports the current transaction. If no transaction existsThrow an exception.
PROPAGATION_REQUIRES_NEW creates a transaction. If a transaction exists, it is suspended.
PROPAGATION_NOT_SUPPORTED performs operations in non-transactional mode. If a transaction exists, the current transaction is suspended.
PROPAGATION_NEVER is executed in non-transaction mode. If a transaction existsThrow an exception.
PROPAGATION_NESTED if the current transaction exists, it is executed as a nested transaction. Otherwise, it will be executed in its own transaction mode.
Required
Supports
Mandatory // command; mandatory
Requires_new
Not_supported
Nerver
Nested
1: PROPAGATION_REQUIRED
Adding the transaction to be executed is not in another transaction, so a new transaction starts.
For example, ServiceB. the transaction level of methodB is defined as PROPAGATION_REQUIRED. serviceA. methodA has started the transaction and calls ServiceB. methodB, ServiceB. methodB sees that it is already running in ServiceA. within a methodA transaction, there will be no new transactions. However, if ServiceA. methodA finds that he is not in the transaction, it will assign a transaction to him. In this way, the transaction will be rolled back if an exception occurs in ServiceA. methodA or anywhere in ServiceB. methodB. Even if the ServiceB. methodB transaction has been committed, ServiceA. methodA will roll back in the next fail, and ServiceB. methodB will also roll back.
2: PROPAGATION_SUPPORTS
If the current transaction is running in the form of a transaction, if there is no longer a transaction, it will run in the form of a non-transaction.
3: PROPAGATION_MANDATORY
It must be run in a transaction. That is, it can only be called by one parent transaction. Otherwise, an exception is thrown.
4: PROPAGATION_REQUIRES_NEW
This is a detour. For example, we designed ServiceA. the transaction level of methodA is PROPAGATION_REQUIRED, ServiceB. the transaction level of methodB is PROPAGATION_REQUIRES_NEW. When it is executed to ServiceB. in methodB, ServiceA. the transaction where methodA is located will be suspended, ServiceB. methodB starts a new transaction and waits for ServiceB. after the methodB transaction is completed, he continues to execute.The difference between the PROPAGATION_REQUIRED transaction and PROPAGATION_REQUIRED is the transaction rollback degree.. Because ServiceB. methodB is a new transaction, there are two different transactions. If ServiceB. methodB has been submitted, ServiceA. methodA fails to roll back, And ServiceB. methodB will not roll back. If ServiceB. methodB fails to roll back, if the exception it throws is caught by ServiceA. methodA, The ServiceA. methodA transaction may still be committed.
5: PROPAGATION_NOT_SUPPORTED
Transactions are currently not supported. For example, ServiceA. the transaction level of methodA is PROPAGATION_REQUIRED, and ServiceB. the transaction level of methodB is PROPAGATION_NOT_SUPPORTED. in methodB, ServiceA. the transaction of methodA is suspended, but it is finished in a non-transaction state, and then continues ServiceA. methodA transactions.
6: PROPAGATION_NEVER
Cannot run in transactions. Assume that the transaction level of ServiceA. methodA is PROPAGATION_REQUIRED, and that of ServiceB. methodB is PROPAGATION_NEVER, then ServiceB. methodB throws an exception.
7: PROPAGATION_NESTED
The key to understanding Nested is savepoint. The difference between him and PROPAGATION_REQUIRES_NEW is,PROPAGATION_REQUIRES_NEW another transaction will be independent from its parent transaction, while the Nested transaction and its parent transaction are dependent on each other, his submission must be done with his parent transaction. That is to say, if the parent transaction is finally rolled back, it will also be rolled back..
The advantage of the Nested transaction is that it has a savepoint. That is, ServiceB. if methodB fails to roll back, ServiceA. methodA also rolls back to the savepoint, ServiceA. methodA can select another branch, such as ServiceC. methodC, continue to execute, to try to complete your own transactions. However, this transaction is not defined in the EJB standard.
Another reference: http://fruitking.iteye.com/blog/201772 (This blog focuses on Learning)