Database transaction isolation level There are 4 isolation levels for database transactions, from low to high:
These four levels can solve the problems of dirty read, non-repeatable read, and phantom read one by one. √: May appear ×: Will not appear
Note: We discuss the isolation level scenario mainly when multiple transactions are concurrent. Dirty reads, phantom reads, and non-repeatable reads Dirty read: Dirty read means that when a transaction is accessing data and modifying the data, and this modification has not been submitted to the database, another transaction also accesses the data and uses the data. Non-repeatable read: It means reading the same data multiple times within a transaction. Before this transaction ends, another transaction also accesses the same data. Therefore, between the two reads in the first transaction, the data read twice by the first transaction may be different due to the modification of the second transaction. This means that the data read twice in a transaction is different, so it is called non-repeatable read. (That is, the same data content cannot be read) Phantom reading: It refers to a phenomenon that occurs when transactions are not executed independently. For example, the first transaction modifies the data in a table, and this modification involves all data rows in the table. At the same time, the second transaction also modifies the data in this table by inserting a new row of data into the table. Then, it may happen that the user operating the first transaction finds that there are still unmodified data rows in the table, as if he were hallucinating. example: surface: CREATE TABLE `cc_wsyw126_user_test_isolation_copy` ( `id` int(11) NOT NULL AUTO_INCREMENT, `password` varchar(64) NOT NULL, `age` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `ix_age` (`age`) )ENGINE=MyISAM DEFAULT CHARSET=utf8; Simulated data: INSERT INTO `cc_wsyw126_user_test_isolation_copy` (`password`, `age`) VALUES ('1', 1), ('twenty two), ('3', 3), ('4', 4); The first transaction A: start transaction insert into cc_wsyw126_user_test_isolation_copy (password, age) values ('5',5) commit The second transaction B: start transaction update cc_wsyw126_user_test_isolation_copy set age = 2 where password >='2' select * from cc_wsyw126_user_test_isolation_copy where password >= '2'; commit Steps to reproduce: As long as the insert statement of transaction A is before the select statement and after the update statement of transaction B, it will be fine. The MySQL InnoDB storage engine implements a multi-version concurrency control protocol - MVCC (Multi-Version Concurrency Control) plus a next-key locking strategy to prevent phantom reads at the Repeatable Read (RR) isolation level. If you want to test phantom reads, experiment with MyISAM. In a clustered index (primary key index), if there is a unique constraint, InnoDB downgrades the default next-key lock to a record lock. Thank you for reading, I hope it can help you, thank you for your support of this site! You may also be interested in:
|
<<: How to encapsulate the table component of Vue Element
>>: Introduction to Nginx log management
The significance of writing order Reduce browser ...
Today I will share with you how to write a player...
Replication is to transfer the DDL and DML operat...
<br /> CSS syntax for table borders The spec...
Table of contents 1. Problem 2. Solution Option 1...
Compared with other large databases such as Oracl...
Table of contents MySQL Truncate usage 1. Truncat...
clear:both is used to清除浮動This is the impression I...
Table of contents Requirements: Implementation st...
binlog is a binary log file that records all DML ...
Problem background: There is a requirement to tak...
Trigger Introduction A trigger is a special store...
First, what is box collapse? Elements that should...
<br />Information duplication, information o...
Today I made a Spring Festival gold coin red enve...