在診斷關於鎖的問題的時候,要查到持有者,被堵塞者。往往是很麻煩,如果你沒有積累的腳本,靠手工查要查很多表,如v$lock,v$process p, v$session s, v$latchholder等。現在有個簡單的方法,使用Oracle的內置腳本生成兩個視圖dba_waiters和dba_blockers來查堵塞。執行內置腳本要在SYS中執行,如下:
C:Documents and Settingsguogang>sqlplus / as sysdba;
SQL*Plus: Release 10.2.0.1.0 - Production on 星期五 1月 24 08:26:42 2014
Copyright (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 。