mysql transaction Repeatable Read

Before that, I introduced  Read Uncommitted , the lowest transaction level of mysql isolation level , and also introduced  Read Committed , a transaction isolation level that may have non-repeatable read (Non Repeatable Read) . Today, I will introduce a relatively higher mysql transaction isolation level Repeatable Read .

Under the Repeatable Read isolation level, a transaction may encounter the problem of phantom read (Phantom Read).

Phantom reading means that in a transaction, the first time a record is queried, it is found that there is no record, but when trying to update this record that does not exist, it can be successful, and when the same record is read again, it is magical appeared.

Let's test it, first prepare a student table with data:

Then, open two MySQL client connections respectively, and execute transaction A and transaction B in sequence:

time Transaction A Transaction B
1 SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
2 BEGIN; BEGIN;
3 SELECT * FROM student WHERE id = 4;
4 INSERT INTO student (id, name) VALUES (4, '张三');
5 COMMIT;
6 SELECT * FROM student WHERE id = 4;
7 UPDATE student SET name = '李四' WHERE id = 4;
8 SELECT * FROM student WHERE id = 4;
9 COMMIT;

 When transaction B reads the record for the first time in step 3 id=4, the read record is empty, indicating that id=4the record does not exist. Subsequently, transaction A inserts a id=4record in step 4 and submits it. When transaction B reads the record again in step 6 id=4, the read record is still empty, but when transaction B tries to update this non-existent record in step 7, it succeeds, and transaction B succeeds in step 8 When the step reads the record again id=4, the record appears.

It can be seen that a phantom read is a record that has not been read. It is thought that it does not exist, but in fact it can be updated successfully. Moreover, after the update is successful, read it again and it will appear.

  Compared with Read Uncommitted and Read Committed  , the Repeatable Read isolation level is relatively higher, and there will be no dirty reads and non-repeatable reads, but phantom reads will occur.

To sum it up:

  • Dirty read: Read data that has not been committed by other transactions.
  • Non-repeatable read: The previously read data is changed by other transactions, and the read will be different from the original.
  • Phantom reading: One transaction inserts a certain piece of data, another transaction finds that it is not there when it reads the inserted piece of data, and it succeeds when updating this record, and the record appears when it is queried again.

Dirty reading is because shared locks are not added when reading, and non-repeatable reading is because two-stage locking is not followed, that is, locks cannot be applied after the lock is released, and all lock applications must be released before release. Phantom reading may be due to the granularity of the lock. If the two-stage lock model is strictly followed, even if an element that does not exist is read, the element must be locked. If this is the case, the insert statement will fail, and there will be no phantom read problem.

Guess you like

Origin blog.csdn.net/lwpoor123/article/details/126765647