[url=http://blog.youkuaiyun.com/v123411739/article/details/39298127]幻读和不可重复读的区别[/url]
不可重复读
不可重复读的重点是修改:
同样的条件, 你读取过的数据, 再次读取出来发现值不一样了
例子:
在事务1中,Mary 读取了自己的工资为1000,操作并没有完成
Java代码 收藏代码
con1 = getConnection();
select salary from employee empId ="Mary";
在事务2中,这时财务人员修改了Mary的工资为2000,并提交了事务.
Java代码 收藏代码
con2 = getConnection();
update employee set salary = 2000;
con2.commit();
在事务1中,Mary 再次读取自己的工资时,工资变为了2000
Java代码 收藏代码
//con1
select salary from employee empId ="Mary";
在一个事务中前后两次读取的结果并不致,导致了不可重复读。
幻读
幻读的重点在于新增或者删除
同样的条件, 第1次和第2次读出来的记录数不一样
例子:
目前工资为1000的员工有10人。
事务1,读取所有工资为1000的员工。
Java代码 收藏代码
con1 = getConnection();
Select * from employee where salary =1000;
共读取10条记录
这时另一个事务向employee表插入了一条员工记录,工资也为1000
Java代码 收藏代码
con2 = getConnection();
Insert into employee(empId,salary) values("Lili",1000);
con2.commit();
事务1再次读取所有工资为1000的员工
Java代码 收藏代码
//con1
select * from employee where salary =1000;
共读取到了11条记录,这就产生了幻像读。
不可重复读
不可重复读的重点是修改:
同样的条件, 你读取过的数据, 再次读取出来发现值不一样了
例子:
在事务1中,Mary 读取了自己的工资为1000,操作并没有完成
Java代码 收藏代码
con1 = getConnection();
select salary from employee empId ="Mary";
在事务2中,这时财务人员修改了Mary的工资为2000,并提交了事务.
Java代码 收藏代码
con2 = getConnection();
update employee set salary = 2000;
con2.commit();
在事务1中,Mary 再次读取自己的工资时,工资变为了2000
Java代码 收藏代码
//con1
select salary from employee empId ="Mary";
在一个事务中前后两次读取的结果并不致,导致了不可重复读。
幻读
幻读的重点在于新增或者删除
同样的条件, 第1次和第2次读出来的记录数不一样
例子:
目前工资为1000的员工有10人。
事务1,读取所有工资为1000的员工。
Java代码 收藏代码
con1 = getConnection();
Select * from employee where salary =1000;
共读取10条记录
这时另一个事务向employee表插入了一条员工记录,工资也为1000
Java代码 收藏代码
con2 = getConnection();
Insert into employee(empId,salary) values("Lili",1000);
con2.commit();
事务1再次读取所有工资为1000的员工
Java代码 收藏代码
//con1
select * from employee where salary =1000;
共读取到了11条记录,这就产生了幻像读。
本文通过具体的数据库操作案例,详细解释了事务隔离级别中幻读与不可重复读的概念及区别。幻读指的是由于其他事务插入数据而导致第一次与第二次查询结果集不一致的现象;而不可重复读则是指在同一个事务内,对同一数据进行多次读取时其值发生变化的情况。
827

被折叠的 条评论
为什么被折叠?



