同事在测试库上对一个表加字段,提示 ORA-00054, 资源忙。 应该是表对象的锁没有释放。
有关Oracle 锁的说明,参考:
ORACLE锁机制
http://blog.csdn.net/tianlesoftware/article/details/4696896
用如下SQL 查看一下系统中相关对象上锁的情况:
/* Formatted on 2012/2/13 14:24:32 (QP5 v5.185.11230.41888) */ SELECT S.SID SESSION_ID, S.USERNAME, DECODE (LMODE, 0, ' None ', 1, ' Null ', 2, ' Row-S(SS) ', 3, ' Row-X(SX) ', 4, ' Share', 5, 'S/Row-X (SSX) ', 6, 'Exclusive ', TO_CHAR (LMODE)) MODE_HELD, DECODE (REQUEST, 0, ' None ', 1, ' Null ', 2, ' Row-S(SS) ', 3, ' Row-X(SX) ', 4, ' Share', 5, 'S/Row-X (SSX) ', 6, 'Exclusive ', TO_CHAR (REQUEST)) MODE_REQUESTED, O.OWNER || ' . ' || O.OBJECT_NAME || ' ( ' || O.OBJECT_TYPE || ' ) ' AS OBJECT_NAME, S.TYPE LOCK_TYPE, L.ID1 LOCK_ID1, L.ID2 LOCK_ID2 FROM V$LOCK L, SYS.DBA_OBJECTS O, V$SESSION S WHERE L.SID = S.SID AND L.ID1 = O.OBJECT_ID AND object_name = 'xxxx';
该SQL 显示所有对象上的锁,如果要查某个具体的对象,可以根据OBJECT_NAME 字段进行一下过滤,找到对应的SID 之后去查V$SESSION 视图。
该视图会显示session 对应的信息,包括终端的信息,如果找到了终端,可以让它提交或者回滚一下就OK了。 我这里是测试环境,直接把session kill 掉了。然后修改表就ok了。
SQL>alter system kill session 'SID,SERIAL#'
后来测试了发现,上面的脚本有时候会查不到对象的的相关记录,故增加了V$ACCESS视图,通过对这个视图进行对象的判断,修改之后的脚本如下:
SELECT S.SID SESSION_ID, S.USERNAME, DECODE (LMODE, 0, ' None ', 1, ' Null ', 2, ' Row-S(SS) ', 3, ' Row-X(SX) ', 4, ' Share', 5, 'S/Row-X (SSX) ', 6, 'Exclusive ', TO_CHAR (LMODE)) MODE_HELD, DECODE (REQUEST, 0, ' None ', 1, ' Null ', 2, ' Row-S(SS) ', 3, ' Row-X(SX) ', 4, ' Share', 5, 'S/Row-X (SSX) ', 6, 'Exclusive ', TO_CHAR (REQUEST)) MODE_REQUESTED, O.OWNER || ' . ' || O.OBJECT_NAME || ' ( ' || O.OBJECT_TYPE || ' ) ' AS OBJECT_NAME, S.TYPE LOCK_TYPE, L.ID1 LOCK_ID1, L.ID2 LOCK_ID2 FROM V$LOCK L, SYS.DBA_OBJECTS O, V$SESSION S,V$ACCESS A WHERE L.SID = S.SID AND L.ID1 = O.OBJECT_ID AND S.SID=A.SID AND A.OBJECT= 'PROC_VALIDATE_RULE_V3';
再次修改了一下,添加了V$SQL视图,这样可以一起查出具体导致这种锁的SQL语句,一次性就搞定了,SQL如下:
/* Formatted on 2012/6/6 10:59:49 (QP5 v5.185.11230.41888) */ SELECT distinct S.SID SESSION_ID, S.STATUS, S.USERNAME, DECODE (LMODE, 0, ' None ', 1, ' Null ', 2, ' Row-S(SS) ', 3, ' Row-X(SX) ', 4, ' Share', 5, 'S/Row-X (SSX) ', 6, 'Exclusive ', TO_CHAR (LMODE)) MODE_HELD, DECODE (REQUEST, 0, ' None ', 1, ' Null ', 2, ' Row-S(SS) ', 3, ' Row-X(SX) ', 4, ' Share', 5, 'S/Row-X (SSX) ', 6, 'Exclusive ', TO_CHAR (REQUEST)) MODE_REQUESTED, O.OWNER || ' . ' || O.OBJECT_NAME || ' ( ' || O.OBJECT_TYPE || ' ) ' AS OBJECT_NAME, S.TYPE LOCK_TYPE, L.ID1 LOCK_ID1, L.ID2 LOCK_ID2, S2.SQL_TEXT FROM V$LOCK L, SYS.DBA_OBJECTS O, V$SESSION S, V$ACCESS A, V$SQL S2 WHERE L.SID = S.SID AND L.ID1 = O.OBJECT_ID AND S.SID = A.SID AND S2.HASH_VALUE = S.SQL_HASH_VALUE AND A.OBJECT = 'PROC_VALIDATE_RULE_V3';
效果如下:
-------------------------------------------------------------------------------------------------------