oracle表锁住 解锁办法

oracle会话被锁是经常的。可以使用alter system kill session 'sid,serial#'杀死会话,

如果服务器是Linux还可以使用杀死Linux上对应的进程.上网查到了2篇详细的解决方法。

就转了过来:

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.查出锁定objectsession的信息以及被锁定的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.查出锁定表的sessionsid, serial#,os_user_name, machine name, terminal和执行的语句

--比上面那段多出sql_textaction

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 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

 

下面是根据需要改写后的脚本,生成了相应的killl命令:

--4.查出锁定objectsession的信息以及被锁定的object,生成kill

select b.owner,

       b.object_name,

       l.session_id,

       x.SERIAL#,

       x.username,

       x.logon_time,

       l.locked_mode,

       'alter system kill session ''' || x.sid || ',' || x.serial# || '''' killsql

  from v$locked_object l, dba_objects b, v$session x

 where b.object_id = l.object_id

   and l.SESSION_ID = x.SID

 order by x.LOGON_TIME asc;

 

 

 

--5.查出锁定objectsession的信息以及被锁定的object,生成killLinux系统kill

select b.owner,

       b.object_name,

       l.session_id,

       x.SERIAL#,

       x.username,

       x.logon_time,

       l.locked_mode,

       'alter system kill session ''' || x.sid || ',' || x.serial# || '''' killsql,

       c.spid AS os_process_id,c.pid,

       'ps -ef | grep ' ||c.spid as check_os_process_command,

       'kill -9 ' ||c.spid as kill_os_process_command

  from v$locked_object l, dba_objects b, v$session x,v$process c

 where b.object_id = l.object_id

   and l.SESSION_ID = x.SID

   and c.addr=x.paddr

 order by x.LOGON_TIME asc;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/665930/viewspace-684109/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/665930/viewspace-684109/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值