MySQL isolation level (phantom read)

Basic understanding of Mysql transactions

ACID
1: The concept
of transaction What is a transaction? A transaction refers to a single logical unit that completes a series of operations, and it either executes successfully or fails. Transaction processing can ensure that only transaction unit operations. Otherwise, there will be no permanent data-oriented update resources. If a logical unit of work wants to become a transaction, then it must have the characteristics of a transaction.

2: The purpose of the
transaction The transaction mainly deals with a large number of operations and the data complexity is relatively high. Here is a common example in life, such as bank transfers.
3: ACID characteristic
1: Atomicity
All operations in a transaction must be atomic. During the execution of this transaction, it either succeeds or fails. It will not stay at a certain link. During the execution of the transaction, he will be rolled back to the execution state of the transaction.
2: Consistency
Before and after the transaction, the integrity of the database will not be destroyed.
3: Isolation
At the same time, only one transaction is allowed to operate on one transaction. The operations between different transactions are not affected by each other.
4: Persistence
After the transaction operation is completed, the impact on the physical level is permanent. After the transaction is committed, it will save all operation updates to the database. And rollback cannot be performed.

Transaction isolation level
Read Uncommitted: Even if the update statement of a transaction is not committed, other transactions can read the change. It will cause the above abnormal conditions to occur, which is extremely error-prone, has no safety at all, and is basically not used.
Read Committed: A transaction can only see the committed updates of other transactions, but not the uncommitted updates. This is the default isolation level of most databases, such as Oracle and Sqlserver.
Repeatable Read (Repeatable Read): Two or more read operations of the same database in a transaction, the result is the same. This is the default isolation level of Mysql database.
Serializable: When a transaction is executed, other transactions are not allowed to execute concurrently, but a completely serialized read. As long as there is a read, writing is prohibited, but it can be read at the same time, eliminating phantom reads. This is the highest level of transaction isolation, although the most secure, but the efficiency is too low, generally not used.

Insert picture description here
The isolation level of MySQL is repeatable read.

How the transaction is implemented

原子性 持久性 一致性都是通过数据库的redo log(重做日志,用来保证原子性和持久性)和undo log(回滚日志,事务的一致性)去完成的。而事物的隔离性则是通过mysql自身的锁机制去完成。

Phantom reading

Let's create a table casually here

CREATE TABLE t_demo(
    id int(11) NOT NULL,
    d int(11) DEFAULT NULL,
    d_1 int(11) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `d_1` (`d_1`)
);
insert into t_demo values(0,0,0),(1,1,1),
(2,2,2),(3,3,3),(4,4,4),(5,5,5);

such as:

  sqlselect * from name where id = 5 for update#提交
  commit;

However, after we execute this statement, the problem arises.
After our transaction is committed, should the record with id 5 be locked, or the data in this table?
Might as well, let's test it.
If we customize the id as 5, when only the record with id 5 is locked

select * from test where id = 5 for update;

#结果555

update  t_demo set d_1 = 5 where id = 0;  


#结果
(0,0,5) (5,5,5)

insert into t_demo VALUES(6,6,5);

select * from t_demo where d_1=5 for update;

(0,0,5)(5,5,5)(6,6,5)


When the transaction is operating, we query the data with id 5 above. When we use locked read, take a look at other data. Therefore, this situation is a phantom reading.

To solve the phantom read solution, we can set the automatic commit of the transaction;

#首先先查看事物的隔离级别
select @@tx_isolation;
#然后设置自动提交
set autocommit;

Guess you like

Origin blog.csdn.net/weixin_43298696/article/details/112305524