在诊断关于锁的问题的时候,要查到持有者,被堵塞者。往往是很麻烦,如果你没有积累的脚本,靠手工查要查很多表,如v$lock,v$process p, v$session s, v$latchholder等。现在有个简单的方法,使用Oracle的内置脚本生成两个视图dba_waiters和dba_blockers来查堵塞。执行内置脚本要在SYS中执行,如下:
C:\Documents and Settings\guogang>sqlplus / as sysdba;
SQL*Plus: Release 10.2.0.1.0 - Production on 星期五 1月 24 08:26:42 2014Copyright (c) 1982, 2005, Oracle. All rights reserved.
连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> @?/rdbms/admin/catblock.sql
视图已创建。
同义词已创建。
.........................
实验:
模拟DML锁
session1:
select * from test for update;
session2:
select * from test for update;
session3:
SQL> select * from dba_waiters;
WAITING_SESSION HOLDING_SESSION LOCK_TYPE MODE_HELD MODE_REQUESTED LOCK_ID1 LOCK_ID2
--------------- --------------- ------------------------- ---------------------- ----------------------- -------------------- --------------- ---------
142 150 Transaction Exclusive Exclusive 131099 2550
SQL> select * from dba_blockers;
HOLDING_SESSION
---------------
150
可以清晰的看到锁的持有者是session 150,等待者是142 。
模拟DDL锁
session1:
select * from obj$ for update
session2:
create index ind_t_id on test(object_id);
session3:
SQL> select * from dba_waiters;
WAITING_SESSION HOLDING_SESSION LOCK_TYPE MODE_HELD MODE_REQUESTED LOCK_ID1 LOCK_ID2
--------------- --------------- -------------------------- ---------------------------------------- ---------------------------------------- ---------- ----------
150 145 Transaction Exclusive Exclusive 1310729 1391
SQL> select * from dba_blockers;
HOLDING_SESSION
---------------
145
可以清晰的看到锁的持有者是session
145,等待者是150 。