• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

oracle查询锁表与解锁情况提供解决方案

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

如果发生了锁等待,我们可能更想知道是谁锁了表而引起谁的等待
以下的语句可以查询到谁锁了表:

复制代码 代码如下:

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

以下的语句可以查询到谁在等待:
复制代码 代码如下:

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

解锁命令:
复制代码 代码如下:

alter system kill session 'sid,serial#'

1).
复制代码 代码如下:

select LOCK_INFO.OWNER || '.' || LOCK_INFO.OBJ_NAME as "已鎖物件名稱", --物件名稱(已經被鎖住)
LOCK_INFO.SUBOBJ_NAME as "已鎖子物件名稱", -- 子物件名稱(已經被鎖住)
SESS_INFO.MACHINE as "機器名稱", -- 機器名稱
LOCK_INFO.SESSION_ID as "會話ID", -- 會話SESSION_ID
SESS_INFO.SERIAL# as "會話SERIAL#", -- 會話SERIAL#
SESS_INFO.SPID as "OS系統的SPID", -- OS系統的SPID
(SELECT INSTANCE_NAME FROM V$INSTANCE) "實例名SID", --實例名SID
LOCK_INFO.ORA_USERNAME as "ORACLE用戶", -- ORACLE系統用戶名稱
LOCK_INFO.OS_USERNAME as "OS用戶", -- 作業系統用戶名稱
LOCK_INFO.PROCESS as "進程編號", -- 進程編號
LOCK_INFO.OBJ_ID as "對象ID", -- 對象ID
LOCK_INFO.OBJ_TYPE as "對象類型", -- 對象類型
SESS_INFO.LOGON_TIME as "登錄時間", -- 登錄時間
SESS_INFO.PROGRAM as "程式名稱", -- 程式名稱
SESS_INFO.STATUS as "會話狀態", -- 會話狀態
SESS_INFO.LOCKWAIT as "等待鎖", -- 等待鎖
SESS_INFO.ACTION as "動作", -- 動作
SESS_INFO.CLIENT_INFO as "客戶資訊" -- 客戶資訊
from (select obj.OWNER as OWNER,
obj.OBJECT_NAME as OBJ_NAME,
obj.SUBOBJECT_NAME as SUBOBJ_NAME,
obj.OBJECT_ID as OBJ_ID,
obj.OBJECT_TYPE as OBJ_TYPE,
lock_obj.SESSION_ID as SESSION_ID,
lock_obj.ORACLE_USERNAME as ORA_USERNAME,
lock_obj.OS_USER_NAME as OS_USERNAME,
lock_obj.PROCESS as PROCESS
from (select *
from all_objects
where object_id in (select object_id from v$locked_object)) obj,
v$locked_object lock_obj
where obj.object_id = lock_obj.object_id) LOCK_INFO,
(select SID,
SERIAL#,
LOCKWAIT,
STATUS,
(select spid from v$process where addr = a.paddr) spid,
PROGRAM,
ACTION,
CLIENT_INFO,
LOGON_TIME,
MACHINE
from v$session a) SESS_INFO
where LOCK_INFO.SESSION_ID = SESS_INFO.SID
order by LOCK_INFO.SESSION_ID;

2).
复制代码 代码如下:

select sql_text
from v$sqltext
where address in (select sql_address from v$session where sid = &sid)
order by piece;

3).
复制代码 代码如下:

ALTER SYSTEM KILL SESSION '會話ID,會話SERIAL#';

4).
kill -9 OS系統的SPID


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
mysql实用技巧之比较两个表是否有不同数据的方法分析发布时间:2022-02-08
下一篇:
基于postgresql行级锁for update测试发布时间:2022-02-08
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap