Oracle中鎖(lock)的用法

 更新時間:2022年05月26日 15:47:21   作者:springsnow  
這篇文章介紹了Oracle中鎖(lock)的用法?,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

數據庫鎖介紹: http://www.gpbgw.com/article/248863.htm

一、查詢oracle鎖定的表:

1、鎖相關表

  • SELECT * FROM v$lock; 
    列:ADDR:鎖定狀態對象地址;KADDR:鎖地址;SID :會話id;ID1:鎖標識符#1;ID2:鎖標識符#2;LMODE:會話持有的鎖模式(0~6);REQUEST:進程請求的鎖模式(0~6); 
    CTIME:當前模式的時間;BLOCK:為1代表阻礙者,表示正在阻礙其它會話;
  • SELECT * FROM v$locked_object;
  • SELECT * FROM v$session;
  • SELECT * FROM v$session_wait;
  • SELECT * FROM v$sqlarea;
  • SELECT * FROM v$process ;
  • SELECT * FROM all_objects

2、常用語句

1、查出鎖定object的session的信息以及被鎖定的object名

SELECT l.session_id sid, s.serial#, l.locked_mode, l.oracle_username, l.os_user_name, s.machine, s.terminal, o.object_name, s.logon_time
FROM v$locked_object l, all_objects o, v$session s
WHERE l.object_id = o.object_id
AND l.session_id = s.sid
ORDER BY sid, s.serial#;

2、查出鎖定表的session的sid, serial#,os_user_name, machine name, terminal和執行的語句,比上面那段多出sql_text和action

SELECT l.session_id sid, s.serial#, l.locked_mode, l.oracle_username, s.user#, l.os_user_name, s.machine, s.terminal, a.sql_text, a.action
FROM v$sqlarea a, v$session s, v$locked_object l
WHERE l.session_id = s.sid
AND s.prev_sql_addr = a.address
ORDER BY sid, s.serial#;

3、查出鎖定表的sid, serial#,os_user_name, machine_name, terminal,鎖的type,mode

SELECT s.sid, s.serial#, s.username, s.schemaname, s.osuser, s.process, s.machine, s.terminal, s.logon_time, l.TYPE
FROM v$session s, v$lock l
WHERE s.sid = l.sid
AND s.username IS NOT NULL
ORDER BY sid;

4、這個語句將查找到數據庫中所有的DML語句產生的鎖,還可以發現,任何DML語句其實產生了兩個鎖,一個是表鎖,一個是行鎖。

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;

5、如果發生了鎖等待,我們可能更想知道是誰鎖了表而引起誰的等待,以下的語句可以查詢到誰鎖了表,而誰在等待。

以下查詢結果是一個樹狀結構,如果有子節點,則表示有等待發生。如果想知道鎖用了哪個回滾段,還可以關聯到V$rollname,其中xidusn就是回滾段的USN

SELECT LPAD(' ', DECODE(l.xidusn, 0, 3, 0)) || l.oracle_username user_name, o.owner, o.object_name, o.object_type, s.sid, s.serial#
FROM v$locked_object l, dba_objects o, v$session s
WHERE l.object_id = o.object_id
AND l.session_id = s.sid
ORDER BY o.object_id, xidusn DESC

3、kill session語句:

alter system kill session 'sid,serial#';

alter system kill session'494,7355';

二、一個有用查找腳本:

1、找到某表的鎖 所屬的sid,alter system kill session 'sid,serial#';即可

select v$lock.sid, decode(v$lock.type, 'MR', 'Media Recovery', 'RT', 'Redo Thread', 'UN', 'User Name', 'TX', 'Transaction', 'TM', 'DML', 'UL', 'PL/SQL User Lock', 'DX', 'Distributed Xaction', 'CF', 'Control File', 'IS', 'Instance State', 'FS', 'File Set', 'IR', 'Instance Recovery', 'ST', 'Disk Space Transaction', 'TS', 'Temp Segment', 'IV', 'Library Cache Invalida-tion', 'LS', 'Log Start or Switch', 'RW', 'Row Wait', 'SQ', 'Sequence Number', 'TE', 'Extend Table', 'TT', 'Temp Table', 'Unknown') LockType, rtrim(owner) || '.' ||
        object_name object_name, decode(lmode, 0, 'None', 1, 'Null', 2, 'Row-S', 3, 'Row-X', 4, 'Share', 5, 'S/Row-X', 6, 'Exclusive', 'Unknown') LockMode, decode(request, 0, 'None', 1, 'Null', 2, 'Row-S', 3, 'Row-X', 4, 'Share', 5, 'S/Row-X', 6, 'Exclusive', 'Unknown') RequestMode, ctime, block b
from v$lock, all_objects
where sid > 6
and v$lock.id1 = all_objects.object_id;

2、查出被lock 的對象,然后 alter system kill session 'sid,serial#';

select object_id, session_id, serial#, oracle_username, os_user_name, s.process
from v$locked_object a, v$session s
where a.session_id = s.sid;

三、LOCK TABLE

1、語法:

LOCK   TABLE   table_1   [,table_2,   ...,   table_n]   IN   lock_mode   MODE
  NOWAIT

2、變量:

table_1,...,table_n: 一系列你想通過使用LOCK TABLE語句鎖住的數據庫表。

lock_mode: 對于某一數據庫表你要設定的鎖定模式。你可以從如下的鎖定模式中任選一個。

  • EXCLUSIVE
  • SHARE ROW EXCLUSIVE
  • SHARE
  • SHARE UPDATE
  • ROW SHARE
  • ROW EXCLUSIVE

NOWAIT: Oracle will not wait to lock the given Table(s), if the Table(s) is(are) not available

3、例子:

LOCK   TABLE   loan     IN   SHARE   MODE   ;
LOCK   TABLE   region   IN   EXCLUSIVE   MODE   NOWAIT;
LOCK   TABLE   acct     IN   SHARE   UPDATE   MODE;
LOCK   TABLE   bank     IN   ROW   EXCLUSIVE   MODE   NOWAIT;
LOCK   TABLE   user     IN   SHARE   ROW   EXCLUSIVE   MODE;
LOCK   TABLE   branch   IN   ROW   SHARE   MODE   NOWAIT;
commit

到此這篇關于Oracle鎖(lock)的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

美丽人妻被按摩中出中文字幕