Seven transactions of Spring and seven transactions of spring
Spring transactions include seven levels: REQUIREDREQUIRED is the default transaction level of Spring. If a transaction exists, the current transaction is supported. If no transaction exists, a new transaction is started. The annotation @ Transactional (rollbackFor = Exception. class, propagation = Propagation. REQUIRED) is added to the method. The following several levels are the same. SUPPORTS the current transaction if there is a transaction. If no transaction exists, the transaction is not executed. However, for the transaction manager for transaction synchronization, SUPPORTS is slightly different from not using transactions.
If a transaction already exists in MANDATORY, the current transaction is supported. If no active transaction exists, an exception is thrown.
REQUIRES_NEW always starts a new transaction. If a transaction already exists, the transaction will be suspended. NOT_SUPPORTED always executes non-transactions and suspends any existing transactions.
NEVER always executes non-transactional operations. If an active transaction exists, an exception is thrown.
NESTED if an active transaction exists, it runs in a NESTED transaction. If no active transaction exists, it is executed according to the REQUIRED attribute.
Data Verification Based on the previous example of using mybatis to connect to the database in Spring boot, the following data is obtained by constantly changing the transaction level:
Call method A transaction |
Method B transaction called |
Whether method A captures Method B |
An exception is thrown when method A is called. |
An exception is thrown when Method B is called. |
Whether the call method data is imported into the database |
Whether the data of the called method is imported into the database |
Conclusion |
REQUIRED |
REQUIRED |
No |
No |
Yes |
No |
No |
The two methods are in the same transaction. If there is a transaction in the context, join the transaction. If no, create a new transaction. |
REQUIRED |
REQUIRED |
Yes |
No |
Yes |
No |
No |
REQUIRED |
SUPPORTS |
No |
No |
Yes |
No |
No |
When there is a transaction in the context, it is added to the transaction. If there is no transaction, no transaction is performed, If an error occurs in the method called in real time, no rollback operation will be performed. |
NEVER |
SUPPORTS |
No |
No |
Yes |
Yes |
Yes |
REQUIRED |
MANDATORY |
No |
No |
Yes |
No |
No |
The error "No existing transaction found for transaction marked with propagation 'mandatory '" is reported, indicating that a transaction is included in the mandatory context. If not, the preceding error is reported. |
NEVER |
MANDATORY |
No |
No |
No |
Yes |
No |
REQUIRED |
REQUIRES_NEW |
No |
No |
Yes |
No |
No |
REQUIRES_NEW each time a new transaction is required. If the sub-transaction throws an exception and the parent transaction captures the sub-transaction exception, the parent transaction will not be rolled back; otherwise, both transactions will be rolled back; if an exception is thrown by the parent transaction after the parent transaction is called, the child transaction will not be rolled back, but the parent transaction will be rolled back. |
REQUIRED |
REQUIRES_NEW |
Yes |
No |
Yes |
Yes |
No |
REQUIRED |
REQUIRES_NEW |
No |
Yes |
No |
No |
Yes |
REQUIRED |
NOT_SUPPORTED |
No |
No |
Yes |
No |
Yes |
If the NOT_SUPPORTED transaction method is defined and the transaction is committed no matter whether an error occurs or not, method A is called. If Method B is called and the exception of Method B is captured, method A commits the transaction, otherwise, roll back the transaction. |
REQUIRED |
NOT_SUPPORTED |
No |
Yes |
No |
No |
Yes |
REQUIRED |
NOT_SUPPORTED |
Yes |
No |
Yes |
Yes |
Yes |
REQUIRED |
NEVER |
No |
No |
No |
No |
No |
"Existing transaction found for transaction marked with propagation 'never '" is reported directly. |
REQUIRED |
NESTED |
No |
No |
Yes |
No |
No |
NESTED: NESTED transactions. If a sub-transaction has an exception and the parent transaction has caught exceptions in the sub-transaction, the sub-transaction will be rolled back, and the parent transaction will not be rolled back; otherwise, all transactions will be rolled back; as long as the parent transaction encounters an exception, the child transaction of the parent transaction will be rolled back, which is different from REQUIRES_NEW; |
REQUIRED |
NESTED |
Yes |
No |
Yes |
Yes |
No |
REQUIRED |
NESTED |
No |
Yes |
No |
No |
No |
|
See http://blog.csdn.net/linuu/article/details/51006780
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.