两个会话上同时执行如下创建Procedure的脚本引发的library cache lock

两个会话上同时执行如下创建Procedure的脚本

begin
  for idx in 1 .. 100000000  loop
    execute immediate 'create or replace procedure lib_test
    is 
    begin
      null;
      end;
      ';
      end loop;
      end;

抓取等待事件:
select SAMPLE_TIME,
       SESSION_ID,  
       NAME,  
       P1,  
       P2,  
       P3,  
       WAIT_TIME,  
       CURRENT_OBJ#,  
       CURRENT_FILE#,  
       CURRENT_BLOCK#  
  from v$active_session_history ash, v$event_name enm  
 where ash.event# = enm.event# 

1	07-7月 -14 10.08.46.694 上午	1619	library cache lock	2171421424	2197228832	301	0	-1	0	0
2	07-7月 -14 10.08.46.694 上午	1628	library cache lock	2171421424	2197084568	301	0	-1	0	0
3	07-7月 -14 10.08.46.694 上午	1629	library cache lock	2171421424	2193480024	301	0	-1	0	0
4	07-7月 -14 10.08.46.694 上午	1651	log file parallel write	1	5	1	0	-1	0	0
5	07-7月 -14 10.08.45.694 上午	1627	library cache lock	2171421424	2197228096	301	0	-1	0	0
6	07-7月 -14 10.08.45.694 上午	1651	log file parallel write	1	5	1	0	-1	0	0
7	07-7月 -14 10.08.44.684 上午	1629	library cache lock	2171421424	2193480024	301	0	-1	0	0

SQL> col event format a30
SQL> select sid,event,p1,p1raw,p2,p3 from v$session where event not like '%message%';

       SID EVENT				  P1 P1RAW		      P2	 P3
---------- ------------------------------ ---------- ---------------- ---------- ----------
      1619 library cache lock		  2171421424 00000000816D42F0 2197228832	301
      1621 Streams AQ: qmn slave idle wai	   0 00 		       0	  0
	   t

      1627 library cache lock		  2171421424 00000000816D42F0 2197228096	301
      1628 library cache lock		  2171421424 00000000816D42F0 2197084568	301
      1629 library cache lock		  2171421424 00000000816D42F0 2193480024	301


其中P3的值是3(=mode) * 100 + 1(namespace)

因此可知模式是Exclusive,namespace 是1 (table/procedure).即可知,新创建Procedure等对象时,对于该库高速缓冲区对象,应该以Exclusive

模式获得library cache lock.

Mode
1=Null 2=Shared 3=Exclusive

SQL> show user
USER is "SYS"
SQL> select kglnaobj "Object" from x$kglob
where kglhdadr='00000000816D42F0'  2  ;

Object
------------------------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------
LIB_TEST



这里的kglhdadr=p1raw

SQL> select saddr,sid,username,event,p1raw from v$session where event='library cache lock';

SADDR			SID USERNAME			   EVENT			  P1RAW
---------------- ---------- ------------------------------ ------------------------------ ----------------
000000008990B688       1619 TEST			   library cache lock		  00000000816D42F0
00000000899161C8       1627 TEST			   library cache lock		  00000000816D42F0
0000000089917730       1628 TEST			   library cache lock		  00000000816D42F0
0000000089918C98       1629 TEST			   library cache lock		  00000000816D42F0

查询持有library cache lock的会话以及lock住的对象
SQL> l
  1  select user_name,kglnaobj "Owner",kgllkses saddr,kgllkreq req,kgllkmod mod,kglnaobj object
  2   from x$kgllk lock_a
  3   where kgllkmod > 0
  4   and exists (select lock_b.kgllkhdl from x$kgllk lock_b
  5   where
  6    lock_a.kgllkhdl = lock_b.kgllkhdl
  7*  and kgllkreq > 0)
 USER_NAME	     Owner		  SADDR 		  REQ	     MOD OBJECT
-------------------- -------------------- ---------------- ---------- ---------- ------------------------------------------------------------
TEST		     LIB_TEST		  0000000089918C98	    0	       3 LIB_TEST
TEST		     LIB_TEST		  00000000899161C8	    0	       3 LIB_TEST
TEST		     LIB_TEST		  000000008990B688	    0	       3 LIB_TEST
TEST		     LIB_TEST		  0000000089917730	    0	       3 LIB_TEST

从mod列可以判断话持有的lock模式为1(如果没记错的话数字1表示null),MOD3 表示以Exclusive 模式获得library cache lock

然后根据sid去查找对应执行的SQL语句

原文地址:https://www.cnblogs.com/hzcya1995/p/13352228.html