關於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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 關於ORACLE鎖的總結Oracle
- Oracle的鎖表與解鎖Oracle
- ORACLE鎖的總結Oracle
- 關於資料庫鎖的總結資料庫
- oracle 鎖表、解鎖的語句Oracle
- MySql關於鎖的一些總結MySql
- InnoDB常用鎖總結(行鎖、間隙鎖、臨鍵鎖、表鎖)
- oracle表鎖住 解鎖辦法Oracle
- 關於oracle的行級鎖Oracle
- Java與Mysql鎖相關知識總結JavaMySql
- 對於Oracle鎖的一些理論總結Oracle
- 【問答分享第一彈】MySQL鎖總結:MySQL行鎖、表鎖、排他鎖、共享鎖的特點MySql
- 關於鎖的幾個動態檢視的總結
- 關於oracle死鎖的模擬Oracle
- 關於enq: TX - row lock contention行鎖的總結ENQ
- MySQL information_schema庫中關於三個關於鎖相關表的欄位解釋彙總MySqlORM
- mysql鎖與事務總結MySql
- Oracle的鎖機制歸納總結Oracle
- oracle 檢視鎖表和解鎖Oracle
- oracle檢視被鎖的表和解鎖Oracle
- Oracle 解決鎖表問題Oracle
- 關於 鎖的四種狀態與鎖升級過程 圖文詳解
- 關於樂觀鎖與悲觀鎖的實際應用
- oracle 查詢鎖 && 解鎖Oracle
- Oracle查詢鎖、解鎖Oracle
- sqlserver鎖表、解鎖、檢視銷表SQLServer
- ORACLE 鎖表的解決方法及查詢引起鎖表SQL語句[轉]OracleSQL
- ORACLE 鎖表的解決方法及查詢引起鎖表SQL語句方法OracleSQL
- 關於Oracle死鎖處理方法Oracle
- MySQL鎖總結MySql
- package鎖的查詢與解鎖Package
- 檢視oracle被鎖的表是誰鎖的Oracle
- 鎖的總結筆記筆記
- ORACLE表和行的鎖Oracle
- Oracl 發生鎖表--解鎖sqlSQL
- Oracle資料表死鎖的解決方法Oracle
- 關於鎖的快速定位
- 轉貼:Oracle的鎖機制歸納總結Oracle