1級鎖有:Select,有時會在v$locked_object出現。
2級鎖有:Select for update,Lock For Update,Lock Row Share
select for update當對話使用for update子串打開一個游標時,所有返回集中的資料行都將處于行級(Row-X)獨占式鎖定,其他物件只能查詢這些資料行,不能進行update、delete或select for update操作。
3級鎖有:Insert, Update, Delete, Lock Row Exclusive
沒有commit之前插入同樣的一條記錄會沒有反應, 因爲後一個3的鎖會一直等待上一個3的鎖, 我們必須釋放掉上一個才能繼續工作。
4級鎖有:Create Index, Lock Share
locked_mode爲2,3,4不影響DML(insert,delete,update,select)操作, 但DDL(alter,drop等)操作會提示ora-00054錯誤。
00054, 00000, "resource busy and acquire with NOWAIT specified"
// *Cause: Resource interested is busy.
// *Action: Retry if necessary.
5級鎖有:Lock Share Row Exclusive
具體來講有主外鍵約束時update / delete ... ; 可能會産生4,5的鎖。
6級鎖有:Alter table, Drop table, Drop Index, Truncate table, Lock Exclusive
以DBA角色, 查看當前資料庫裏鎖的情况可以用如下SQL語句:
col owner for a12
col object_name for a16
select b.owner,b.object_name,l.session_id,l.locked_mode
from v$locked_object l, dba_objects b
where b.object_id=l.object_id
/
select t2.username,t2.sid,t2.serial#,t2.logon_time
from v$locked_object t1,v$session t2
where t1.session_id=t2.sid order by t2.logon_time
/
SELECT /*+ rule */ s.username, decode(l.type,'TM','TABLE LOCK', 'TX','ROW LOCK', NULL) LOCK_LEVEL, o.owner,o.object_name,o.object_type, s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser FROM v$session s,v$lock l,dba_objects o WHERE l.sid = s.sid AND l.id1 = o.object_id(+) AND s.username is NOT NULL
Cause: The session specified in an ALTER SYSTEM KILL SESSION command cannot be killed immediately because the session is involved in a non-interruptible operation (for example, rolling back a transaction or being blocked by a network operation). The session has been marked to be killed as soon as possible after the current operation is done.
没有被立即杀死因为会话在进行不可中断的操作(比如,正在回滚一个事务或者被网络操作阻塞)。当前操作完成,会话马上被标记为killed
Action: No action is required for the session to be killed, but further executions of the ALTER SYSTEM KILL SESSION command on this session may cause the session to be killed sooner.