problem
1. What is a transaction?
2. What is the purpose of the transaction?
Database transaction
The underlying transaction management of Spring is based on database transactions, so before learning Spring transactions, we need to learn database transactions first.
Definition of transaction
Transaction is a logical unit in the execution process of the database management system, consisting of a limited sequence of database operations. (From Wikipedia)
A simple translation is to combine the step-by-step operations of accessing data resources into a whole operation, and this whole execution unit is a transaction .
The purpose of the transaction
The purpose of the transaction is to ensure the consistency of the data.
Characteristics of transactions
To ensure that the step-by-step operation is combined into a whole operation, the whole success or the whole failure (same life and death). There needs to be a strict definition.
The four characteristics of transactions (ACID) : atomicity (Atomicity), consistency (Consistency), isolation (IsoLation), durability (Durability).
- Atomicity: Multiple operations of the same transaction are indivisible minimum units. When all operations are successfully executed, the transaction is submitted, otherwise all successful operations are cancelled and the database is restored to the initial state.
- Consistency: After the transaction is submitted, the data results are consistent with the business expectations rules. For example, no matter whether the transaction is successfully submitted or not, the total of the two accounts is unchanged.
- Isolation: There is isolation between transactions and transactions during concurrent operations, and they do not interfere with each other. Isolation depends on the isolation level of the database.
- Persistence: After the transaction is successfully submitted, a series of operations in the transaction will be persisted to the database.
Transaction isolation level
- Read uncommitted: A transaction can read another uncommitted content. There may be "dirty read", "non-repeatable read", "phantom read".
- Read Commit: A transaction can only read the content of another data that has been submitted successfully. There may be "non-repeatable reading" and "phantom reading". The default isolation level for most databases
- Repeatable reading: the same data is read multiple times throughout the transaction. "Magic reading" may appear. Mysql default level.
- Serialization: All transaction operations must be performed in sequence. The strictest isolation level.
Data concurrency issues
- Dirty read : Transaction 1 updates the data, but it is not committed, but transaction 2 can read the uncommitted content. When the transaction rolls back the uncommitted data, the transaction 2 reads before.
- Non-repeatable read : The same transaction reads the same data multiple times, and the result of each read is different. Transaction 1 reads the data before and after the update operation of transaction 2, and the two results will be different.
- Magic read : the same transaction performs multiple queries on the same data set, and the result set is different each time . Transaction 1 reads the data before and after the transaction 2 insert or delete operation, and the result set of the operation is different.
Reason for dirty read: "select" operation is not restricted
Reason for non-repeatable read: "update" operation is not limited
Reasons for phantom reading: "insert" and "delete" operations are not restricted
The effect of transaction isolation level on data concurrency issues
- Read uncommitted role: no effect.
- Read submission: Using "snapshot read" can avoid "dirty read", but cannot avoid "non-repeatable read" and "phantom read".
- Repeatable reads: Use "snapshot reads" to avoid "dirty reads" and "non-repeatable reads", but not "magic reads".
- Serialization: Can avoid "dirty read", "non-repeatable read", "phantom read".
Snapshot read
Snapshot reading is based on MVCC and undo log implementation, suitable for simple select statements. MVCC concurrent version control is achieved by ReadView (transaction view).
Read has been submitted: Each SQL in the transaction will generate a ReadView. The value in the latest ReadView will be read during the query operation.
Repeatable read: A ReadView is generated when the transaction starts. Multiple SQL in the transaction will read the same ReadView. The value in the same ReadView will also be read in multiple SQL queries.
Spring transaction management
Spring provides a unified programming template for transaction management and establishes a unified transaction abstraction at a high level.
Transaction management interface
TransactionDefinition
It is used to describe the specific attributes of the transaction such as the isolation level of the transaction, the timeout period, whether it is a read transaction, and the propagation rules of the transaction.
Transaction isolation level
In TransactionDefinition, five constants representing the isolation level are defined:
(1) TransactionDefinition.ISOLATION_DEFAULT, which means that the default isolation sector of the database is used (Oracle uses read submission, Mysql uses repeatable reads by default)
(2) TransactionDefinition.ISOLATION_READ_UNCOMMITTED, indicating that the read is not submitted.
(3) TransactionDefinition.ISOLATION_READ_COMMITTED, which means read and submit.
(4) TransactionDefinition.ISOLATION_REPEATABLE_READ, which can be read repeatedly.
(5) TransactionDefinition.ISOLATION_SERIALIZABLE, which means serialization.
Transaction propagation behavior
When a transaction is called by another transaction, you need to specify the transaction propagation.
TransactionDefinition.PROPAGATION_REQUIRED, if there is currently a transaction, then join the transaction; if there is no transaction, then create a new transaction, this is the most common choice.
TransactionDefinition.PROPAGATION_SUPPORTS, if there is currently a transaction, then join the transaction; if there is no transaction, then continue to run in a non-transactional way.
TransactionDefinition.PROPAGATION_MANDATORY, if there is currently a transaction, then join the transaction; if there is currently no transaction, throw an exception.
Transaction timeout
Exceed the time that the transaction is allowed to run, the transaction will be automatically rolled back if the transaction is not completed within the specified time. The default unit in TransactionDefinition is seconds.
Read-only status
The transaction only reads the transaction resources and does not modify any data. If you only read the transaction resource data, you can set it to read-only state to improve running performance.
Rollback rules
Used to define the corresponding exceptions for rollback and not rollover. By default, transactions are rolled back only when they encounter specific exceptions during runtime.
TransactionStatus
Used to indicate the specific running status of the transaction. The transaction manager can obtain the status information of the transaction runtime through this interface, and can also roll back the transaction indirectly through this interface, which is more meaningful than rolling back the transaction when an exception is thrown.
PlatformTransactionManager
Transaction manager, provide transaction commit, rollback and return an existing transaction or create a new transaction from the transaction environment according to the transaction definition information.
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
reference
[1] https://zh.wikipedia.org/wiki/Database transaction
[2]https://baijiahao.baidu.com/s?id=1611918898724887602&wfr=spider&for=pc
[3]https://www.cnblogs.com/AlmostWasteTime/p/11466520.html
[4]https://juejin.im/post/5b00c52ef265da0b95276091
[5] "Mastering Spring4.x Enterprise Application Development Combat"