關於ORACLE的鎖表與解鎖總結
總結1:的鎖表與解鎖
select
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;
--kill session語句
alter system kill session'50,492';
--以下幾個為相關表
SELECT * FROM v$lock;
SELECT * FROM v$sqlarea;
SELECT * FROM v$session;
SELECT * FROM v$process ;
SELECT * FROM v$locked_object;
SELECT * FROM all_objects;
SELECT * FROM v$session_wait;
--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;
這個語句將查詢到中所有的DML語句產生的鎖,還可以發現,
任何DML語句其實產生了兩個鎖,一個是表鎖,一個是行鎖。
殺鎖命令
alter system kill session 'sid,serial#'
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
如果發生了鎖等待,我們可能更想知道是誰鎖了表而引起誰的等待
以下的語句可以查詢到誰鎖了表,而誰在等待。
以上查詢結果是一個樹狀結構,如果有子節點,則表示有等待發生。
如果想知道鎖用了哪個回滾段,還可以關聯到V$rollname,其中xidusn就是回滾段的USN
col user_name format a10
col owner format a10
col object_name format a10
col object_type format a10
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
總結2:一個有用查詢指令碼:
column sid format 999;
column b format 9;
column object_name format a30;
column locktype format a20;
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;
找到某表的鎖 所屬的sid.
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 的物件
然後 alter system kill session 'sid,serial#';
知識點3:
LOCK TABLE
語法:
LOCK TABLE table_1 [,table_2, ..., table_n] IN lock_mode MODE
NOWAIT
變數:
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
例子:
SQL
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
/
轉自:http://space.itpub.net/18953778/viewspace-563108
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-623894/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 關於資料庫鎖的總結資料庫
- MySql關於鎖的一些總結MySql
- InnoDB常用鎖總結(行鎖、間隙鎖、臨鍵鎖、表鎖)
- Java與Mysql鎖相關知識總結JavaMySql
- oracle檢視被鎖的表和解鎖Oracle
- 【問答分享第一彈】MySQL鎖總結:MySQL行鎖、表鎖、排他鎖、共享鎖的特點MySql
- oracle 查詢鎖 && 解鎖Oracle
- Oracle查詢鎖、解鎖Oracle
- 檢視oracle被鎖的表是誰鎖的Oracle
- mysql鎖與事務總結MySql
- 關於 鎖的四種狀態與鎖升級過程 圖文詳解
- 關於樂觀鎖與悲觀鎖的實際應用
- MySQL鎖總結MySql
- 【鎖】Oracle鎖系列Oracle
- Oracle:優化方法總結(關於連表查詢)Oracle優化
- 一個關於wait/notify與鎖關係的探究AI
- oracle的鎖Oracle
- 檢視oracle死鎖程式並結束死鎖Oracle
- 分散式鎖總結分散式
- java裡的鎖總結(synchronized隱式鎖、Lock顯式鎖、volatile、CAS)Javasynchronized
- golang中關於死鎖的思考與學習Golang
- 關於分散式鎖原理的一些學習與思考-redis分散式鎖,zookeeper分散式鎖分散式Redis
- MySQL行鎖、表鎖、間隙鎖,你都瞭解嗎MySql
- Oracle死鎖一例(ORA-00060),鎖表導致的業務死鎖問題Oracle
- 全域性鎖、表鎖、行鎖
- JUC鎖種類總結
- 一文搞懂MySQL行鎖、表鎖、間隙鎖詳解MySql
- MySQL 悲觀鎖與樂觀鎖的詳解MySql
- SqlServer關於分割槽表的總結SQLServer
- mysql關於表空間的總結MySql
- mysql關於臨時表的總結MySql
- Python提高:關於GIL(全域性直譯器鎖)與執行緒互斥鎖的理解Python執行緒
- 【Oracle】死鎖的產生與處理Oracle
- oracle鎖級別相關測試Oracle
- Oracle-解鎖scott賬戶Oracle
- mysql關於memory引擎的表的總結MySql
- 詳解 MySql InnoDB 中的三種行鎖(記錄鎖、間隙鎖與臨鍵鎖)MySql
- MySQL/InnoDB中,樂觀鎖、悲觀鎖、共享鎖、排它鎖、行鎖、表鎖、死鎖概念的理解MySql
- oracle事務transaction鎖lock一點兒小思考或總結Oracle