原文: ORA-00060 deadlock detected while waiting for resource.
Cause: Your session and another session are waiting for a resource locked by the other. This condition is known as a deadlock. To resolve the deadlock, one or more statements were rolled back for the other session to continue work.
Action Either: 1.Enter a ROLLBACK statement and re-execute all statements since the last commit or 2.Wait until the lock is released, possibly a few minutes, and then re-execute the rolled back statements.
ORA-00060:等待资源的时候检测到死锁。
原因:A进程和B进程同时等待彼此所占用的资源,这就是所谓的死锁。解决死锁的思路是,回滚A进程的一条或多条语句,等待B进程完成后再执行。
方案(两者皆可):回滚至最近一次提交,重新执行所有语句。或者,等待自动解锁,这个可能需要一些时间,解锁后,再执行语句。
其实,Oracle提供的方案,多少有点保守,一般惯用方法:找到这个进程,直接kill掉!
实验室:模拟死锁
1.模拟死锁实验:
1.1 主表
-- Create table
create table WDZ1
(
WDZ1ID NUMBER not null,
MEMO VARCHAR2(20)
)
;
alter table WDZ1
add constraint XXXXXX primary key (WDZ1ID);
1.2 从表(没有外健的索引)
-- Create table
create table WDZ2
(
WDZ2ID NUMBER not null,
WDZ1ID NUMBER,
MEMO VARCHAR2(20)
)
;
-- Create/Recreate primary, unique and foreign key constraints
alter table WDZ2
add constraint XXXXX primary key (WDZ2ID)
;
alter table WDZ2
add constraint XXX foreign key (WDZ1ID)
references WDZ1 (WDZ1ID);
1.3 插入数据表到住表
begin
insert into wdz1 values (1,'aa');
insert into wdz1 values(2,'aa2');
insert into wdz1 values (3,'aa3');
insert into wdz2 values(10,3,'wdz3--1');
commit;
end;
1.4 在一个数据库seeesion里面插入数到从表,但是不提交事务
begin
update wdz2 set memo='update wdz2 momo'
where wdz2id=10;
insert into wdz2 values(20,2,'wdz2--1');
end;
对从表进行插入/修改记录,施加的锁也就是行级锁
1.5 在另外一个数据库seeesion里面删除 主表数据
delete from wdz1 where wdz1id=1
这时候,程序会死锁,报错ORA-00060,除非上面的对从表的数据操作提交事务或者回滚事务。
2. 具体原因分析
一个数据表的外键主要有3种方式来维护它自己和主表数据的一致性。
(1)delete cascade
例子如下:
alter table WDZ2
add constraint XXX foreign key (WDZ1ID)
references WDZ1 (WDZ1ID) on delete cascade;
(2)Set null
例子如下:
alter table WDZ2
add constraint XXX foreign key (WDZ1ID)
references WDZ1 (WDZ1ID) on delete set null;
(3)No action
注意,这是oracle外键使用时候的默认选项。
例子如下:
alter table WDZ2
add constraint XXX foreign key (WDZ1ID)
references WDZ1 (WDZ1ID);
以前出现死锁主要是我们认为,在 1。4。会对数据表wdz2进行施加行级锁,但是从表(wdz2)的外健是No action,删除主表(wdz1)不会去访问从表,更不会去锁定 wdz2表的记录或者对整个数据从表(wdz2)施加表级锁。事实上 oralce的No action 选项的字面意思欺骗了我们,oracle在删除主表的时候会去寻找所有以主表的主键作为外键的数据表,然后看去看从表是否有该外键的索引,如果没有则会对整个从表施加表级锁,然后对从表进行全表扫描。当然如果从表存在外键的索引,会去访问对应的索引,而不会对从表本身进行加锁。
