
区别: 不可重复读:同样的条件下,读取过的数据,当我们再次读取时值发生了变化。 幻读:同样的条件下,第1次和第2次读出来的记录数不一样。 具体分析: 1、不可重复读 同样的条件下,读取过的数据,当我们再次读取时值发生了变化。 例子: 在事务1中,A读取了自己的工资为1000,但是此时事务1的操作还并没有完成 ,后面还有1次相同的读取操作。 con1 = getConnection();
select salary from employee where employeeName ="A"; 在事务2中,这时财务人员修改了A的工资为2000,并提交了事务。 con2 = getConnection();
update employee set salary = 2000 where employeeName = "A";
con2.commit(); 在事务1中,A再次读取自己的工资时,工资变为了2000 。 select salary from employee where employeeName ="A"; 在一个事务中前后两次读取的结果并不致,导致了不可重复读。 2、幻读 同样的条件下,第1次和第2次读出来的记录数不一样。 例子: 目前工资为1000的员工有5人。 事务1,读取所有工资为1000的员工,共读取10条记录 。 con1 = getConnection();
Select * from employee where salary =1000; 这时另一个事务向employee表插入了一条员工记录,工资也为1000 con2 = getConnection();
Insert into employee(employeeName,salary) values("B",1000);
con2.commit(); 事务1再次读取所有工资为1000的员工,共读取到了6条记录,这就产生了幻读。 //con1
select * from employee where salary =1000; |