Oracle数据库的锁类型
oracle官方文档里面关于锁的定义:Locks are mechanisms that prevent destructive interaction between transactions accessing the same resource—either user objects such as tables and rows or system objects not visible to users, such as shared data structures in memory and data dictionary rows. 这段话的大概意思是说锁的作用用来保护tables、rows、shared data structures in memory and data dictionary rows等在交互访问的时候不会被破坏。
Alter table、Drop able、Drop index、Truncate table 、Lock exclusive
1.关于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
--我们主要关注TX和TM两种类型的锁
--UL锁用户自己定义的,一般很少会定义,基本不用关注
--其它均为系统锁,会很快自动释放,不用关注
ID1
NUMBER
Lock identifier #1 (depends on type)
ID2
NUMBER
Lock identifier #2 (depends on type)
---当lock type 为TM时,id1为DML-locked object的object_id
---当lock type 为TX时,id1为usn+slot,而id2为seq。
--当lock 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)
--大于0时表示当前会话以某种模式占有该锁,等于0时表示当前会话正在等待该锁资 源,即表示该会话被阻塞。
--往往在发生TX锁时,伴随着TM锁,比如一个sid=9会话拥有一个TM锁,一般会拥有一个 或几个TX锁,但他们的id1和id2是不同的,请注意
REQUEST
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)
--大于0时,表示当前会话被阻塞,其它会话占有改锁的模式
CTIME
NUMBER
Time since current mode was granted
BLOCK
NUMBER
The lock is blocking another lock
0, 'Not Blocking', /* Not blocking any other processes */
1, 'Blocking', /* This lock blocks other processes */
2, 'Global', /* This lock is global, so we can't tell */