首頁 > 軟體

Oracle中鎖(lock)的用法

2022-05-26 18:02:13

資料庫鎖介紹: https://www.jb51.net/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)的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com