Introduction and example analysis of spring transaction isolation level
This paper mainly studies the introduction and examples of spring transaction isolation level, as follows.
When two transactions operate on records in the same database, what is the impact between them? This brings up the concept of transaction isolation level. The isolation of database is closely related to concurrency control. The isolation level of a database is part of the transaction attribute acid of the database. Acid, that is, atomicity, consistency, isolation, and durability. Spring has four transaction isolation levels: read_ UNCOMMITTED、READ_ COMMITTED、REPEATABLE_ Read and serializable. Another is the default isolation level of the database, default, and MySQL is repeatable by default_ READ。
Let's take a specific look.
READ_ UNCOMMITTED
As the name suggests, read_ Uncommitted means that one transaction can read uncommitted transaction records of another transaction. In other words, a transaction can read the data that is still uncommitted by other transactions. This is the weakest isolation level for spring transactions. As shown in the figure below, transaction a starts and writes a record. At this time, transaction B reads the data and reads the record. However, after that, transaction a rolls back. Therefore, the database is in an invalid state. This condition is called dirty read. In addition to dirty reading, read_ Uncommitted may also have problems with non repeatable read and phantom read.
READ_ COMMITTED
READ_ The committed isolation level indicates that a transaction can only read committed records, not uncommitted records. In other words, a transaction can only read the committed data, and it can't read the uncommitted data Therefore, dirty read will not happen again, but other problems may occur. See the figure below.
Between the two reads of transaction a, transaction B modifies that record and commits it. Therefore, the records read before and after transaction a are inconsistent. This problem is called non repeatable read. (if the records read twice are inconsistent, repeat the reading to find the problem.)
Except for the problem of non repeatable read, read_ The problem of phantom read may also occur in committed.
REPEATABLE_ READ
REPEATABLE_ Read means that a transaction can read a record from the database multiple times, and the records read multiple times are consistent and the same. This isolation level can avoid the problems of dirty read and non repeatable read, but the problem of phantom read may occur. See the figure below.
Transaction a reads a series of records from the database twice, during which transaction B inserts a record and commits it. When transaction a reads the second time, it will read the record just inserted by transaction B. During a transaction, a series of records read twice by transaction a are inconsistent. This problem is called phantom read.
SERIALIZABLE
Serializable is the strongest isolation level in spring. When a transaction is executed, it will be locked at all levels. For example, it will be locked when reading and writing, as if the transaction is carried out in a serial manner, not together. This will prevent dirty read, non repeatable read and phantom read, but it will lead to performance degradation.
DEFAULT
MySQL defaults to repeatable_ READ。
example
Next, let's look at an example. Start a transaction in the database mysql without committing. Then, another transaction reads the record.
At first, the records in the database are as shown in the figure
@H_ 301_ 83@
Next, open transaction a in the database mysql and insert a record.
The transaction attribute of the business class of the service is configured as read_ UNCOMMITTED。
Run the following test class
The results are as follows:
It can be seen that this transaction reads uncommitted data.
At this time, transaction a opened in MySQL is rolled back.
Run the program again and the result is