SQL> create table tbl_ora_60 (
id number(5),
name varchar2(5)
);
SQL> insert into tbl_ora_60 values(1, 'a');
1 row created.
SQL> insert into tbl_ora_60 values(2, 'b');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from tbl_ora_60;
ID NAME
---------- -----
1 a
2 b
实验开始
Session1:
SQL> update tbl_ora_60 set name='c' where id=1;
1 row updated.
Session2:
SQL> update tbl_ora_60 set name='d' where id=2;
1 row updated.
Session1:
SQL> update tbl_ora_60 set name='e' where id=2;
hang住
Session2:
SQL> update tbl_ora_60 set name='f' where id=1;
hang住
此时,Session1:
SQL> update tbl_ora_60 set name='e' where id=2;
update tbl_ora_60 set name='e' where id=2
*
ERROR at line 1:
ORA-00060: deadlock detected while waiting for resource
此时,Session1:
SQL> select * from tbl_ora_60;
ID NAME
---------- -----
1 c
2 b
说明:此处可以证明产生报错后,Oracle自动执行的rollback操作是基于单条SQL,不是整个事务的,所以这里只有id=2的记录被rollback,id=1的执行仍正常。
Session2:
SQL> update tbl_ora_60 set name='f' where id=1;
hang住
继续,Session1:
SQL> commit;
Commit complete.
Session2:
SQL> update tbl_ora_60 set name='f' where id=1;
1 row updated.
Session1:
SQL> select * from tbl_ora_60;
ID NAME
---------- -----
1 c
2 b
只有id=1更新成功。
Session2:
SQL> select * from tbl_ora_60;
ID NAME
---------- -----
1 f
2 d
id=1和id=2都更新成功,但未COMMIT。
SQL> commit;
Commit complete.
Session1:
SQL> select * from tbl_ora_60;
ID NAME
---------- -----
1 f
2 d
因Session2执行COMMIT,提交更新,此处显示与Session执行相同。