Oracle鎖表和解鎖

2021-06-26 21:02:51 字數 3075 閱讀 1762

宣告:本文**

oracle的鎖表與解鎖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

--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 /*+ rule */ 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

oracle 鎖表查詢 和 解鎖

查詢誰鎖定,誰等待 select rule 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...

oracle 檢視鎖表和解鎖

檢視鎖表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 obje...

Oracle檢視鎖表和解鎖

注意許可權問題 1.檢視是否有被鎖的表 select b.owner,b.object name,a.session id,a.locked mode from v locked object a,dba objects b where b.object id a.object id 2.檢視是哪個...