The Spring transaction propagation mechanism includes the following seven types:
1. Propagation.REQUIRED: The default transaction propagation level, which means that if a transaction currently exists, join the transaction; if
If there is no current transaction, create a new one.
2. Propagation.SUPPORTS: If there is a current transaction, join the transaction; if there is no current transaction, use a non-transactional
mode continues to operate.
3. Propagation.MANDATORY: (mandatory: Mandatory) If there is a transaction currently, join the transaction;
If there is no previous transaction, an exception is thrown.
4. Propagation.REQUIRES_NEW: means to create a new transaction, if there is a current transaction, hang the current transaction
rise. That is to say, regardless of whether the external method opens the transaction, the internal method modified by Propagation.REQUIRES_NEW will newly open its own transaction, and the opened transactions are independent of each other and do not interfere with each other.
5. Propagation.NOT_SUPPORTED: Run in non-transactional mode, if there is a current transaction, suspend the current transaction.
6. Propagation.NEVER: Run in a non-transactional mode, and throw an exception if there is a current transaction.
7. Propagation.NESTED: If a transaction currently exists, create a transaction to run as a nested transaction of the current transaction; for example
If there is no transaction currently, this value is equivalent to PROPAGATION_REQUIRED.
The above 7 propagation behaviors can be divided into the following 3 categories according to whether they support the current transaction:
Class 1: Support current transaction
REQUIRED (required)
If the current method does not have a transaction, create a new transaction, if there is already a transaction, join this transaction.
[Analogous couple relationship: Live together if you have a house, and earn money to buy a house if you don’t have a house. (I am willing to accompany you to endure hardships, but I must have a house)]
SUPPORTS (can have)
Support the current transaction, if there is no current transaction, it will be executed in a non-transactional manner.
[If you have a house to live together, if you don’t have a house, you can rent a house (if you have some, it doesn’t matter if you don’t have a house)]
MANDATORY (mandatory)
Use the current transaction, or throw an exception if there is no current transaction.
[Live together if you have a house, break up if you don’t have a house (don’t go through hardships with you, break up if you don’t have a house)]
Class 2: does not support the current transaction
REQUIRES_NEW
Create a new transaction execution, if there is a current transaction, suspend the current transaction.
[Don't want your house, we must make money together to buy a house. (You can’t live in your house, you must buy a new house)]
NOT_SUPPORTED
Perform operations in a non-transactional manner, and suspend the current transaction if there is a current transaction.
[Don’t want your house, we must rent a house together (If we don’t live in your house, we must rent a house)]
NEVER
Executes non-transactionally, throwing an exception if a transaction currently exists.
[You must rent a house together, and break up if you want a house (must rent a house together, break up if you have a house—I don’t like your house, and I have to accompany you to repay the mortgage)]
Class Three: Nested Transactions
NESTED
Executes within a nested transaction, if a transaction currently exists. If there is no current transaction, perform a similar operation to PROPAGATION_REQUIRED.
[If you have a house, let’s use the house as a base to do some small business. If you make money, it’s good to continue to develop. If you lose money, at least you still have a house. It doesn’t matter if you don’t have a house, let’s make money together to buy a house (start a business without risk, save capital and be a sensible couple)]
[Note: The "house" here refers to the business]
The difference between nested transactions (NESTED) and joined transactions (REQUIRED):
If the entire transaction is executed successfully, the results of the two are the same.
If the transaction fails in the middle of execution, the entire transaction will be rolled back after joining the transaction; while the nested transaction will be partially rolled back without affecting
Echoes the result of execution in the previous method.