Several Propagation Features of transactions
1. PROPAGATION_REQUIRED: if a transaction exists, the current transaction is supported. Enable if no transaction exists
2. PROPAGATION_SUPPORTS: if a transaction exists, the current transaction is supported. If no transaction exists, the transaction is not executed.
3. PROPAGATION_MANDATORY: if a transaction already exists, the current transaction is supported. If no active transaction exists, an exception is thrown.
4. PROPAGATION_REQUIRES_NEW: always starts a new transaction. If a transaction already exists, the transaction will be suspended.
5. PROPAGATION_NOT_SUPPORTED: Always executes non-transactions and suspends any existing transactions.
6. PROPAGATION_NEVER: Always executes non-transactional operations. If an active transaction exists, an exception is thrown.
7. PROPAGATION_NESTED: if an active transaction exists, it runs in a nested transaction. If no active transaction exists,
Execute the statement according to the TransactionDefinition. PROPAGATION_REQUIRED attribute.
Isolation level of Spring transactions
1. ISOLATION_DEFAULT: This is a default isolation level of PlatfromTransactionManager, which uses the default transaction isolation level of the database.
The other four correspond to the JDBC isolation level.
2. ISOLATION_READ_UNCOMMITTED: This is the lowest isolation level of the transaction. It allows an external transaction to see the uncommitted data of this transaction.
This isolation level will generate dirty reads, which cannot be repeated and Phantom reads.
3. ISOLATION_READ_COMMITTED: ensure that the data modified by one transaction can be read by another transaction only after it is committed. Another transaction cannot read the uncommitted data of the transaction.
4. ISOLATION_REPEATABLE_READ: This transaction isolation level prevents dirty reads and prevents repeated reads. However, phantom reading may occur.
In addition to ensuring that one transaction cannot read the uncommitted data of another transaction, it also ensures that the following situations are avoided (non-repeated reads ).
5. ISOLATION_SERIALIZABLE this is the most costly but reliable transaction isolation level. Transactions are processed in sequence.
In addition to preventing dirty reads and not repeated reads, Phantom reads are also avoided.
Some concepts are described as follows:
Dirty read:When a transaction is accessing the data and modifying the data, and the modification has not been committed to the database, another transaction also accesses the data, then the data is used. Because the data has not been committed, the data read by another transaction is dirty data, and the operations performed based on the dirty data may be incorrect.
Repeatable read:A transaction reads the same data multiple times. When the transaction is not completed, another transaction also accesses the same data. Therefore, the data read twice in the first transaction may be different because of the modification of the second transaction. In this way, the data read twice in a transaction is different, so it is called non-repeated read.
Phantom read:This refers to a phenomenon that occurs when a transaction is not executed independently. For example, the first transaction modifies the data in a table, which involves all the data rows in the table. At the same time, the second transaction also modifies the data in this table. This modification inserts a new row of data into the table. In the future, the user who operates the first transaction will find that there are still data rows in the table that have not been modified, just like an illusion.