同事在测试库上对一个表加字段,提示 ORA-00054, 资源忙。 应该是表对象的锁没有释放。
有关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';
效果如下:
版权所有,文章允许转载,但必须以链接方式注明源地址,否则追究法律责任!
Email: tianlesoftware@gmail.com
QQ: tianlesoftware@gmail.com
Skype: tianlesoftware
Blog: http://www.tianlesoftware.com
Weibo: http://weibo.com/tianlesoftware
Twitter: http://twitter.com/tianlesoftware
Facebook: http://www.facebook.com/tianlesoftware
Linkedin: http://cn.linkedin.com/in/tianlesoftware
-------加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请----
DBA1 群:62697716(满); DBA2 群:62697977(满) DBA3 群:62697850(满)
DBA 超级群:63306533(满); DBA4 群:83829929 DBA5群: 142216823
DBA6 群:158654907 DBA7 群:172855474 DBA总群:104207940