The difference between phantom read and non-repeatable read

The difference between phantom read and non-repeatable

read
Non- repeatable read The point of non-repeatable read is to modify:
the same condition, the data you have read, read it again and find that the value is different
Example :
In transaction 1, Mary read My own salary is 1000, and the operation has not completed the
Java code. Collection code
con1 = getConnection(); 
select salary from employee empId = "Mary"; 


In transaction 2, the financial staff modified Mary's salary to 2000 and submitted Transaction.
Java code Favorite code
con2 = getConnection(); 
update employee set salary = 2000; 
con2.commit(); 


In transaction 1, when Mary reads her salary again, the salary becomes 2000
Java code Favorite code
//con1 
select salary from employee empId = "Mary"; 


The results of two reads before and after one transaction are inconsistent, resulting in non-repeatable reads. Phantom

reading The key point of phantom reading is to add or delete the same condition, but the number of records read out the first time and the second time are different Example : There are 10 employees with a salary of 1000 at present.




Transaction 1, reads all employees whose salary is 1000.
Java code Collection code
con1 = getConnection(); 
Select * from employee where salary =1000; 
A total of 10 records are read

At this time another transaction inserts an employee record into the employee table, and the salary is also 1000
Java code Collection code
con2 = getConnection (); 
Insert into employee(empId,salary) values("Lili",1000); 
con2.commit(); 


Transaction 1 reads the
Java code of all employees whose salary is 1000 again. Collection code
//con1 
select * from employee where salary =1000; 


A total of 11 records were read, which resulted in a phantom read.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326221659&siteId=291194637