V$LOCK 视图查看当前系统中的锁
Column |
Datatype |
Description |
ADDR |
RAW(4 | 8) |
Address of lock state object |
KADDR |
RAW(4 | 8) |
Address of lock |
SID |
NUMBER |
Identifier for session holding or acquiring the lock |
TYPE |
VARCHAR2(2) |
Type of user or system lock |
The locks on the user types are obtained by user applications. Any process that is blocking others is likely to be holding one of these locks. The user type locks are: |
||
TM - DML enqueue |
||
TX - Transaction enqueue |
||
UL - User supplied |
||
The system type locks are listed in Table 8-1. Be aware that not all types of locks are documented. To find a complete list of locks for the current release, query the V$LOCK_TYPEdata dictionary view, described on "V$LOCK_TYPE". |
||
ID1 |
NUMBER |
Lock identifier #1 (depends on type) |
ID2 |
NUMBER |
Lock identifier #2 (depends on type) |
LMODE |
NUMBER |
Lock mode in which the session holds the lock: |
|
||
0 - none |
||
1 - null (NULL) |
||
2 - row-S (SS) |
||
3 - row-X (SX) |
||
4 - share (S) |
||
5 - S/Row-X (SSX) |
||
6 - exclusive (X) |
||
|
||
REQUST |
NUMBER |
Lock mode in which the process requests the lock: |
0 - none |
||
1 - null (NULL) |
||
2 - row-S (SS) |
||
3 - row-X (SX) |
||
4 - share (S) |
||
5 - S/Row-X (SSX) |
||
6 - exclusive (X) |
||
|
||
CTIME |
NUMBER |
Time since current mode was granted |
BLOCK |
NUMBER |
A value of either 0 or 1, depending on whether or not the lock in question is the blocker. |
V$LOCK 里的type 可以同过视图v$lock_type查看锁的具体含义在10.2.0.1 里有158种锁,到了11.2.01里 锁的种类增加到了202个锁
在官当里对 ID1和ID2的解释很模糊,到底ID1和ID2表示什么呢
根据锁的种类的不同ID1 和ID2所代表的意义不同,我们经常遇到的锁的种类摸过于TX 和TM锁,我们来看看具体含义吧
在TX锁:
ID1对应视图V$TRANSACTION中的XIDUSN字段和XIDSLOT字段。其中ID1的高16位为XIDUSN,低16位为XIDSLOT。其组成形式为: 0xRRRRSSSS ( RRRR = RBS number, SSSS = slot
ID2对应视图V$TRANSACTION中的XIDSQN字段。以十进制表示该slot被重用的次数
xidusn= selecttrunc(id1/power(2,16)) XIDUSN from dual
xidslot=select bitand(id1,65535) from dual
或 xidslot=select bitand(id1,to_number('ffff','xxxx') from dual )
ID1对于与dba_object 里的object_id
ID2: 等于0
SQL> create table t1 as select * from emp where 0=1;
Table created.
SQL> insert into t1 select * From emp;
14 rows created.
SQL> select distinct sid from v$mystat;
SID
----------
159
另外开一个session:
SQL> select * From v$lock where sid=159;
ADDR KADDR SID TY ID1 ID2 LMODE REQUESTCTIME BLOCK
-------- -------- ---------- -- ---------- ---------- ---------- ---------- ---------- ----------
3D1C3F90 3D1C3FA8 159 TM 51373 0 3 0 1330
3D22541C 3D225538 159 TX 131090 259 6 0 1330
SQL> select XIDUSN,XIDSLOT,XIDSQN from V$TRANSACTION where XIDSQN=259;
XIDUSN XIDSLOT XIDSQN
---------- ---------- ----------
2 18 259
SQL> Select trunc(131090/power(2,16)) XIDUSN from dual;
XIDUSN
----------
2
SQL> select bitand(131090,65535) from dual;
BITAND(131090,65535)
--------------------
18
SQL> select object_name from dba_objects where object_id='51373';
OBJECT_NAME
--------------------------------------------------------------------------------------------------------------------------------
T1
锁的视图:
v$lock
v$lock_type
dba_lock