Transaction propagation refers to how the transaction runs when two transactions are called. spring defines seven propagation behaviors, which we will explain respectively
PROPAGATION_REQUIRED
If there is A transaction, the current transaction is supported. If there is no transaction, A new transaction is started. For example, when calling method B alone, A new transaction is started because there is no transaction in the context. However, when method A calls Party B, method B joins the current transaction, that is, the transaction of method A
@Transactional(propagation = Propagation.REQUIRED) public void methodA(){ //todo methodB(); //todo } @Transactional(propagation = Propagation.REQUIRED) public void methodB(){ //todo }
PROPAGATION_SUPPORTS
If there is A transaction that supports the current transaction, if there is no transaction, it will be executed as A non transaction. As shown below, if method B is called separately, it will be executed as A non transaction, but if method A calls method B, it will be added to the transaction of method A
@Transactional(propagation = Propagation.REQUIRED) public void methodA(){ //todo methodB(); //todo } @Transactional(propagation = Propagation.SUPPORTS) public void methodB(){ //todo }
PROPAGATION_MANDATORY
The method must run in A transaction. If the current transaction does not exist, an exception will be thrown. For example, when method B is called alone, an exception will be thrown, and method A must call method B
@Transactional(propagation = Propagation.REQUIRED) public void methodA(){ //todo methodB(); //todo } @Transactional(propagation = Propagation.MANDATORY) public void methodB(){ //todo }
PROPAGATION_REQUIRES_NEW
He will start A new transaction. If A transaction already exists, suspend the existing transaction. For example, if method B is called alone, he will start A transaction himself. However, if method A calls method B, suspend the transaction of method A, and method B starts A new transaction, that is, the two transactions are irrelevant. For example, After method A calls method B, method A fails. Only the contents executed by method A will be rolled back without affecting method B
@Transactional(propagation = Propagation.REQUIRED) public void methodA(){ //todo methodB(); //todo } @Transactional(propagation = Propagation.REQUIRES_NEW) public void methodB(){ //todo }
PROPAGATION_NOT_SUPPORTED
It runs as A non transaction. If there is A transaction, it will suspend the transaction and run as A non transaction. For example, the following two methods. When method A calls method B, method B runs as A non transaction, while calling method B alone also runs as A non transaction
@Transactional(propagation = Propagation.REQUIRED) public void methodA(){ //todo methodB(); //todo } @Transactional(propagation = Propagation.NOT_SUPPORTED) public void methodB(){ //todo }
PROPAGATION_NEVER
It always runs as a non transaction. If there is a transaction, an error will be reported
PROPAGATION_NESTED
It is A nested transaction. If there is no transaction, A transaction will be opened, but if there is A transaction, it will be nested in this transaction and A transaction will be opened. For example, A single call to method B will open A transaction, but if method A opens A transaction, call method B to open A nested transaction. When the outer method A fails, the content of method B will be rolled back, However, if method B fails, it only rolls back its internal content, which will not affect the outer things
@Transactional(propagation = Propagation.REQUIRED) public void methodA(){ //todo methodB(); //todo } @Transactional(propagation = Propagation.NESTED) public void methodB(){ //todo }
Among them, promotion is easy to be confused_ Nested and promotion_ REQUIRES_ New, if both transactions do not exist, start a new transaction, but the deployment_ For transactions started by nested, the outer transaction will affect the inner transaction. The inner transaction is a sub transaction of the outer transaction_ REQUIRES_ New has started two independent transactions, which do not affect each other