Transaction management in spring (2)
Transaction propagation properties
When a transaction method is called by another transaction method, it must specify how the transaction should propagate. For example: the method may continue to run in the existing transaction, or it may start a new transaction and run in its own transaction.
The propagation behavior of a transaction can be specified by the propagation property. Spring defines 7 types of propagation behavior.
Transaction propagation behaviors supported by Spring
REQUIRED propagation behavior
When the purchase() method of bookService is called by another transaction method checkout(), it will run within the existing transaction by default. This default propagation behavior is REQUIRED. So within the start and end boundaries of the checkout() method only A transaction. This transaction is only committed at the end of the checkout() method, resulting in the user not being able to buy a book
Transaction propagation properties can be defined in the propagation property of the @Transactional annotation
REQUIRES_NEW propagation behavior
Another common propagation behavior is REQUIRES_NEW. It means that the method must start a new transaction, running within its own transaction. If there is a transaction running, it should suspend it first.
Configure propagation properties in transaction notifications