Home > Java > javaTutorial > Detailed explanation of Spring transaction isolation level

Detailed explanation of Spring transaction isolation level

黄舟
Release: 2017-03-03 10:18:45
Original
1539 people have browsed it

Five different transaction isolation levels are defined in spring:
1. ISOLATION_DEFAULT (Generally, you can use this configuration) ;

This is a PlatfromTransactionManager The default isolation level uses the databasedefault transaction isolation level.
2. ISOLATION_READ_UNCOMMITTED 4 p" L. I' F; k1 {) a. D( E5 ?: V
This is the lowest isolation level for a transaction, which allows another transaction You can see the uncommitted data of this transaction. This isolation level will produce dirty reads, non-repeatable reads and phantom reads
This situation will not occur with the default transaction isolation level of most databases) 1. N$ G7 Another transaction cannot read the uncommitted data of this transaction. This transaction isolation level can avoid dirty reads, but non-repeatable reads and phantom reads may occur. : U8 m# n8 g1 k( E: N

What is a dirty read? (Modified and not submitted caused )
For example: Zhang San’s salary is 5000, and transaction A changes his salary to 8000, but transaction A has not yet been submitted. At the same time, transaction B is reading Zhang San’s salary, and reads that Zhang San’s salary is 8000. Then, an exception occurs in transaction A. , and the transaction was rolled back. Zhang San's salary was rolled back to 5000. Finally, the data that transaction B read that Zhang San's salary was 8000 was dirty data, and transaction B did a dirty read ' T. , B j d! H# i) s$ i

(This situation will not occur with the default transaction isolation level of most databases) ; N$ ~" J7 a& Y* ?/ G8 I
4. ISOLATION_REPEATABLE_READ This transaction isolation level can prevent dirty reads and non-repeatable reads, but phantom reads may occur. 3 [* {- F2 s. w+ |

What is a non-repeatable read? (Modification caused)
6 h2 E% J, S7 f9 n+ m0 U4 ]" ?% e. aFor example: / ^7 Y# T6 |& Z' B
In transaction A, it is read that Zhang San's salary is 5000. The operation has not been completed and the transaction has not been submitted yet. 5 K: N6 c" [$ p: J4 L/ I" B
At the same time, transaction B changed Zhang San’s salary to 8,000 and submitted the transaction. Subsequently, in transaction A, Zhang San’s salary is read again, and the salary becomes 8,000. The results of two reads in a transaction are inconsistent, resulting in non-repeatable reads. (This situation will not occur with the default transaction isolation level of most databases) . h+ U! y9 {) }1 R8 ~% B, l; t

5. ISOLATION_SERIALIZABLE This is the most expensive but most reliable transaction isolation level. Transactions are processed as sequential execution. In addition to preventing dirty reads and non-repeatable reads, phantom reads are also avoided. % c0 @8 E& g" x1 . |9 w9 N+ ~

For example: A currently has 10 employees with a salary of 5,000. Transaction A reads all 10 employees with a salary of 5,000. At this time, transaction B inserts a salary. It is also a record of 5000. This is when transaction A reads the employee whose salary is 5000, and the record is 11 people. This situation occurs in the default transaction isolation level of most databases. This kind of transaction isolation level will bring table-level locks)
_! O3 W6 M/ LExplanation: Oracle
The default transaction isolation level of the database has been guaranteed Avoid dirty reads and non-repeatable reads. However, phantom reads require table-level locks. The ISOLATION_SERIALIZABLE transaction isolation level must be used carefully in Oracle's default row-level locks. Table-level locks will be used, which has a huge impact on performance. Generally, if there is no special need, just configure it to use the default transaction isolation level of the database.

The above is a detailed explanation of the isolation level of Spring transactions, and more. For related content, please pay attention to the PHP Chinese website (www.php.cn)!

##

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template