Chinaunix首页 | 论坛 | 博客
  • 博客访问: 841385
  • 博文数量: 150
  • 博客积分: 5123
  • 博客等级: 大校
  • 技术积分: 1478
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-06 10:03
文章分类

全部博文(150)

文章存档

2011年(2)

2010年(139)

2009年(9)

分类: Oracle

2010-08-31 17:42:28

create table t(id number primary key,name varchar2(20));
有数据
1      'zhang'
2      'wang'
3      'chen'

开两个sqlplus:
第一个:
update table t set id=4 where id=1;

每二个:
update table t set name='liang' where id=1;

然后在第一个中:
commit;

然后在第二个中:
commit;

这时第二个,更新的行数为0,原因是id=1的记录被前一个事务更新为4了。



SQL> set transaction isolation level serializable;

事务处理集。

SQL> update t set id=4 where id=1;
update t set id=4 where id=1
       *
第 1 行出现错误:
ORA-08177: 无法连续访问此事务处理


关于幻读与一致读,很多oracle 都认为在一致性读下是没有幻读的,实际上这是不正确的。幻读的意思是,在一个事务中,如果两次读的结果是不一样的,则存在幻读,如果多次读,结果都是一致的,则不存在幻读。下面我们看一下oracle中的一致性读是否可以避免幻读呢:
开两个sqlplus:
第一个:
update table test set id=4 where id=1;

每二个:
SQL> update t set content='xxxx' where id=1;

1 row updated.

SQL> select * from test;

ID NAME
---------- ----------------------------------------
1 guoguo
2 pingping
3 yaoyao

SQL> update test set name='guoguo' where id=1;
第二个窗口被hang住,同时可以看到,第一次查询test表,看不到id=4的记录。

然后在第一个窗口中commit
SQL> commit;
Commit complete.
在第二个窗口中,hang住的语句执行完了:
SQL> update test set name='guoguo' where id=1;

0 rows updated.
这时在第二个窗口中,再查test表:
SQL> select * from test;

ID NAME
---------- ----------------------------------------
4 guoguo
2 pingping
3 yaoyao
这时可以看到在id=4的记录,说明在第二个窗口中的一个事务中,两次查询test表,看到的结果是不一样的,说明在oracle的默认事务级别下,是存在幻读的。



阅读(2087) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~