Spring transaction operations and spring transaction operations
The first phase of our project has almost ended, so some details have been taken out. The most common problems are transaction operations. Because I am responsible for a module (because I am the person in charge of another project), the team members often encounter transaction problems, and there will be a lot of wonderful usage and various misuse, it is estimated that they will know the annotation @ Transactional on the method, but many of the details are unknown. So there is often a situation where a large amount of code suffers a transaction problem, and then I will make various changes. So let's make a summary of the transaction today. If you forget it later, you can come back and check it out.
Generally, we pay attention to the following three aspects when using transactions:
1. propagation: propagation
2: isolation: transaction isolation level
3: whether readOnly is read-only
When @ Transactional annotation without any parameters is used separately, set the propagation mode to REQUIRED, the read-only flag to false, and the transaction isolation level to READ_COMMITTED, in addition, the transaction will not be rolled back against the controlled exception (checked exception.
Propagation:
* 3. propagation: Propagation * <br> propagation Value: REQUIRED (default): executed in the transaction state. If no transaction exists, a new transaction is created. SUPPORTS: If transaction exists, it is executed in the transaction state; if there is no transaction currently, it is executed in the non-transaction state; MANDATORY: It must be executed in the transaction state; if there is no transaction currently, it throws the IllegalTransactionStateException; REQUIRES_NEW: create and execute a new transaction. If transaction already exists, the current transaction is suspended. NOT_SUPPORTED: the transaction is executed without the transaction state. If the current transaction already exists, the current transaction is suspended. NEVER: execute without transaction. If transaction already exists, an IllegalTransactionStateException PROPAGATION_NESTED (embedded transaction) is thrown. Create a Savepoint to implement nested transactions. If an exception (unchecked exception) is thrown) the transaction is rolled back to the savepoint without affecting the outer transaction. the rollback of the outer transaction rolls back the internal transaction together; (useful for spring-jdbc and mybatis, but not for sessionFactory of hibernate)
Isolation level
* 1. isolation: transaction isolation level * spring's transaction isolation level ISOLATION_DEFAULT: use the default database isolation level. In spring, the default value is READ_COMMITTED ISOLATION_READ_UNCOMMITTED, which allows reading changed uncommitted data, which may lead to dirty reads, non-repeated reads, and Phantom reads. ISOLATION_READ_COMMITTED: Allows concurrent transactions to be read after being committed, which can avoid dirty reads and may lead to repeated and Phantom reads. ISOLATION_REPEATABLE_READ: The read results of the same field are consistent multiple times, which can lead to phantom read. ISOLATION_SERIALIZABLE: fully complies with ACID principles to ensure that dirty reads, non-repeated reads, and Phantom reads are not performed.
Read-only or not:
6. Whether readOnly is read-only is not read-only by default (false). If there is no transaction, this setting will be ignored.
1. Note that when using the orm framework, we need to use transactions to trigger SQL Execution. Therefore, transactions are used in the orm framework. It is also important that a major part of the typical no session error in hibernate is that no transaction is added.
2. The second note means that if no transaction exists in the selected policy, setting the read-only attribute does not have any effect.
3. Other attributes:
Timeout indicates the response time of a transaction. If the response time exceeds the time limit, an error occurs. Unit: Second noRollbackFor: specifies that the exception class will not be rolled back. By default, transaction rollback is only useful for RuntimeException exceptions (only for non-checked exception rollback) noRollbackForClassName: it is the same as above, but here it is the name of the specified exception value