Database transaction isolation level - the difference between phantom read and non-repeatable read

non-repeatable read

The point of non-repeatable read is to modify:

The same conditions, the data you have read, read it again and find that the value is different

example:

In transaction 1, Mary reads her own salary as 1000, and the operation is not completed 

Java code   Favorite code
  1. con1 = getConnection();  
  2. select salary from employee empId ="Mary";  

 



In transaction 2, the financial staff modified Mary's salary to 2000 and submitted the transaction. 

Java code   Favorite code
  1. con2 = getConnection();  
  2. update employee set salary = 2000;  
  3. con2.commit();  



In transaction 1, when Mary reads her salary again, the salary becomes 2000 

Java code   Favorite code
  1. //con1  
  2. select salary from employee empId ="Mary";  



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

 

 

hallucinations

The point of phantom reading is to add or delete

Under the same conditions, the number of records read out for the first time and the second time is different

example:

There are currently 10 employees with a salary of 1000. 
Transaction 1, reads all employees whose salary is 1000. 

Java code   Favorite code
  1. con1 = getConnection();  
  2. Select * from employee where salary =1000;  

A total of 10 records were read. 

At this time, another transaction inserted an employee record into the employee table, and the salary was also 1000. 

Java code   Favorite code
  1. con2 = getConnection();  
  2. Insert into employee(empId,salary) values("Lili",1000);  
  3. con2.commit();  



Transaction 1 again reads all employees with salary of 1000 

Java code   Favorite code
  1. //con1  
  2. select * from employee where salary =1000;  



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

 

Transaction isolation level:

1. READ_UNCOMMIT reads uncommitted transactions. Transaction A modifies a record that is not committed, and transaction B reads the record, which will lead to dirty reads, non-repeatable reads, and phantom reads.

 

2. READ_COMMIT reads a committed transaction. Transaction A modifies a record, but transaction B does not allow reading. Only when transaction A is committed, transaction B can read the record. Dirty reads can be avoided, but lead to non-repeatable reads and phantom reads.

 

3. REPEATABLE_READ can not only realize the function of READ_COMMIT, but also prevent when A transaction reads a record, B transaction is not allowed to modify it; it prevents dirty reads and non-repeatable reads, but cannot avoid phantom reads.

 

.

4. SERIALIZABLE Transactions at this level are executed sequentially, which can avoid the defects of the above level, but the overhead is large

 

5.DEFAULT: Use the default level of the current database

 

 

 

Guess you like

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