区别:
不可重复读:同样的条件下,读取过的数据,当我们再次读取时值发生了变化。
幻读:同样的条件下,第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;
推荐教程:mysql教程
以上就是幻读和不可重复读有什么区别的详细内容。