This is the 14th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
I. Brief introduction to business
A transaction, in short, is a set of operations that either complete or do not complete. It is never allowed to do only a part of the operation. For example, in bank transfer, 1000 yuan will be transferred from account A to account B, including two operations A-1000 and B+1000. Either these two operations will be completed or none will be done, and only one of them will not be allowed to be performed. (You can think of the consequences if A-1000, but B does not +1000? / squint/squint).
Transaction rollback: When an exception or error occurs during the execution of a transaction, the process that was not started is resumed. For example, in the bank transfer process above, suppose that a-1000 operation has been completed, but when B+1000 operation is performed, the system has A position error, then it needs to return to the state before the transfer operation, that is, A and B have the same amount of money, and no less.
Transaction submission, when a transaction execution process does not occur any exceptions, errors, then we need to save the transaction modification. For example, in the bank transfer process above, if a-1000 and B+1000 operations are all completed without any exceptions or errors, the modification of transaction execution status (A reduces 1000 yuan and B increases 1000 yuan) needs to be saved, namely transaction submission.
Second, transaction properties (ACID)
Transactions have four characteristics, as follows:
All operations in a transaction will either succeed or fail. All operations will either succeed or fail. Data [The set of states after transaction submission is called consistent, that is, the database only contains the state of transaction submission] Concurrency [For any two concurrent transactions A and B, from transaction A’s point of view, B will either end before A begins or after A ends, so that each transaction does not feel that other transactions are executing concurrently.] 【 Durability means that once a transaction is committed, it is saved on hard disk and changes to data in the database are permanent 】
Three, five transaction isolation levels
The Isolation attribute supports a total of five transaction Settings, as described below:
Isolation level | explain |
---|---|
DEFAULT | Using the isolation level set by the database (the default), the isolation level is determined by the default setting of the DBA. |
READ_UNCOMMITTED | Dirty read, unrepeatable read, phantom read (lowest isolation level, high concurrency) |
READ_COMMITTED | Unrepeatable read, phantom read issues (locking rows being read) |
REPEATABLE_READ | Phantom read (locks all rows read) |
SERIALIZABLE | Ensure that all conditions do not occur (lock table) |
Four, seven big transaction propagation attributes
The transaction | explain |
---|---|
PROPAGATION_REQUIRED | Spring’s default transaction propagation level, which supports the current transaction if one exists. If there are no transactions, start a new one. |
PROPAGATION_SUPPORTS | If a transaction exists, the current transaction is supported. If there is no transaction, non-transactional execution. However, for transactionally-synchronized transaction managers, PROPAGATION_SUPPORTS is slightly different from not using transactions. |
PROPAGATION_MANDATORY | If a transaction already exists, support the current transaction. If there is no active transaction, an exception is thrown. |
PROPAGATION_REQUIRES_NEW | Always start a new transaction. If a transaction already exists, suspend the existing transaction. |
PROPAGATION_NOT_SUPPORTED | Always execute nontransactionally and suspend any existing transactions. |
PROPAGATION_NEVER | Always execute nontransactionally, throwing an exception if there is an active transaction |
PROPAGATION_NESTED | If an active transaction exists, it runs in a nested transaction. . If there is no active transaction, then the TransactionDefinition PROPAGATION_REQUIRED attributes, when using PROPAGATION_NESTED, the underlying data source must be based on JDBC 3.0, and implementers need support savepoint transaction mechanism |
Spring provides four transaction features, five isolation levels, and seven propagation behaviors