The essence of Spring transactions is actually the support of transactions by the database. Without the transaction support of the database, spring cannot provide transaction functions. For pure JDBC operation database, if you want to use transactions, you can follow the following steps:
Get the connection Connection con = DriverManager.getConnection()
Open transaction con.setAutoCommit(true/false);
Execute CRUD
Commit transaction/rollback transaction con.commit() / con.rollback();
Close the connection conn.close();
After using Spring’s transaction management function, we can not Then write the code for steps 2 and 4, and let Spirng automatically complete . Then Spring is How to open and close transactions before and after the CRUD we write? To solve this problem, we can understand the implementation principle of Spring's transaction management as a whole. The annotation method is as an example
#.##Configuration fileEnable annotationsDriver, and mark the relevant classes and methods with the @Transactional annotation
is shown in the following table:
Constant Explanation | ##PROPAGATION_REQUIRED | |||||||||||||||||||||||||||
PROPAGATION_REQUIRES_NEW | ||||||||||||||||||||||||||||
throws an exception | , outer transaction capture, you can also not process the rollback operationPROPAGATION_SUPPORTS | |||||||||||||||||||||||||||
PROPAGATION_MANDATORY | ||||||||||||||||||||||||||||
PROPAGATION_NOT_SUPPORTED | ||||||||||||||||||||||||||||
PROPAGATION_NEVER | ||||||||||||||||||||||||||||
PROPAGATION_NESTED | ||||||||||||||||||||||||||||
If an active transaction exists, run in a nested transaction. If there is no active transaction, the REQUIRED attribute is executed. It uses a single transaction with multiple savepoints that can be rolled back. Rollback of internal transactions will not affect external transactions. It only works on the DataSourceTransactionManager transaction manager. |
Isolation level | Isolation level value | Problems caused |
Read-Uncommitted | 0 | Causes dirty read |
Read-Committed | 1 | Avoid dirty reads, allow non-repeatable reads and phantom reads |
Repeatable-Read | 2 | Avoid dirty reads, non-repeatable reads, Allow phantom reading |
Serializable | 3 | Serialized reading, transactions can only be executed one by one, avoiding dirty reads, non-repeatable reads, Phantom reading. The execution efficiency is slow, so use it with caution |
Dirty read: One transaction added, deleted, or modified data, but it was not committed. Another transaction can read the uncommitted data. If the first transaction is rolled back at this time, the second transaction will read dirty data.
Non-repeatable read: Two read operations occurred in a transaction. Between the first read operation and the second operation, another transaction modified the data. At this time, the data read twice is inconsistent.
Phantom reading: The first transaction batches modifications to a certain range of data, and the second transaction adds a piece of data to this range. At this time, the first transaction will lose the modification of the newly added data.
Summary:
The higher the isolation level, the more complete and consistent the data can be guaranteed, but the greater the impact on concurrency performance.
The default isolation level of most databases is Read Commited, such as SqlServer and Oracle
The default isolation level of a few databases is: Repeatable Read, for example: MySQL InnoDB
Explanation | |
This is the default isolation level of PlatfromTransactionManager, using the default transaction isolation level of the database. The other four correspond to JDBC isolation levels. | |
This is the lowest isolation level for a transaction, which allows another transaction to see the uncommitted data of this transaction. This isolation level will produce dirty reads, non-repeatable reads and phantom reads. | |
Ensure that the data modified by one transaction can only be read by another transaction after it is submitted. Another transaction cannot read the uncommitted data of this transaction. | |
This transaction isolation level can prevent dirty reads and non-repeatable reads. However, phantom reads may occur. | |
This is the most expensive but most reliable transaction isolation level. Transactions are processed as sequential execution. |
The above is the detailed content of Detailed introduction to Spring transaction principles. For more information, please follow other related articles on the PHP Chinese website!