1.产生死锁的必要条件
互斥:资源不能被共享,只能一个进程使用
不可剥夺:已经分配的资源不能从相应的进程中被强制性剥夺
循环等待条件:系统中若干进程组成环境,该环路中的每个进程都在等待相邻进程正在占用的资源
2.死锁处理一般策略
鸵鸟算法忽略该问题
检测死锁并恢复
仔细地对资源进行动态分配,以避免死锁
破坏死锁产生的4个必要条件之一,来防止死锁
3.产生死锁例子
oracle2008@ORCL> create table test (id number);
oracle2008@ORCL> create table test2(id number);
oracle2008@ORCL> insert into test(id) values(1);
oracle2008@ORCL> insert into test2(id) values(1);
oracle2008@ORCL> commit;
会话1:oracle2008@ORCL> update test set id='2';
会话2:oracle2008@ORCL> update test2 set id='2';
会话1:oracle2008@ORCL>update test2 set id='2';
会话2:oracle2008@ORCL> update test set id='2';
ORA-00060: deadlock detected while waiting for resource
4.日志文件解析
第一部分:显示哪些进程持有锁和哪些进程正在等待锁资源情况。
对于每一个资源都有两部分内容:blocker(s)和waiter(s)
Resource name:表示被持有或者被等待的锁的名称。名称组成:locked_type-ID1-ID2,
其中locked_type 表示锁类型
ID1表示回滚段
ID2表示事物表条目
Process表示锁等待会话 v$process.pid
Session表示锁等待会话v$session.sid
Holds表示持有锁模式
Waits表示等待的锁模式
Deadlock graph:
---------Blocker(s)-------- ---------Waiter(s)---------
Resource Name process session holds waits process session holds waits
TX-00050014-00000885 21 125 X 27 141 X
TX-00090006-0000083b 27 141 X 21 125 X
session 125: DID 0001-0015-00000057 session 141: DID 0001-001B-0000001E
session 141: DID 0001-001B-0000001E session 125: DID 0001-0015-00000057
第二部分:显示等待资源信息
? Objn:表示dba_objects的object_id
? Rowid:表示每行的rowid
Rows waited on:
Session 125: obj - rowid = 00014A2D - AAAUotAABAABEgRAAA
(dictionary objn - 84525, file - 1, block - 280593, slot - 0)
Session 141: obj - rowid = 00014A2C - AAAUosAABAABEgJAAA
(dictionary objn - 84524, file - 1, block - 280585, slot - 0)
第三部分:显示参与死锁其他会话信息
Information for the OTHER waiting sessions
Session 141:
sid: 141 ser: 56 audsid: 1920394 user: 127/oracle2008 flags: 0x100045
pid: 27 O/S info: user: oracle, term: UNKNOWN, ospid: 5323
image: oracle@oracle2008.com (TNS V1-V3)
client details:
O/S info: user: oracle, term: pts/3, ospid: 5322
machine: localhost.localdomain program: sqlplus@oracle2008.com (TNS V1-V3)
application name: SQL*Plus, hash value=3669949024
current SQL:
update test set id='2'
第四部分:显示造成死锁ora-00060的sql语句
Information for THIS session:
----- Current SQL Statement for this session (sql_id=cfz75twg0xn5t) -----
update test2 set id='2'