Oracle中查看引起Session阻塞的2个脚本分享
用户A执行删除,但是没有提交。
SQL> delete from test where object_id<10;
已删除8行。
用户B执行删除或者更新id<10的记录,则被阻塞。
SQL> update test set flag=’N’ where object_id<10;
遇到这种阻塞,首先需要确定问题。可以使用以下脚本。
select t2.username,t2.sid,t2.serial#,t2.logon_time
from v$locked_object t1,v$session t2
where t1.session_id=t2.sid order by t2.logon_time;
from v$locked_object t1,v$session t2
where t1.session_id=t2.sid order by t2.logon_time;
结果如下:
USERNAME SID SERIAL# LOGON_TIME
—————————— ———- ———- ————–
LIHUILIN 14 87 09-11月-13
LIHUILIN 139 655 09-11月-13
—————————— ———- ———- ————–
LIHUILIN 14 87 09-11月-13
LIHUILIN 139 655 09-11月-13
或者使用
select
(select username from v$session where sid=a.sid) blocker,
a.sid,’ is blocking ‘,
(select username from v$session where sid=b.sid) blockee,
b.sid
from v$lock a,v$lock b
where a.block=1 and b.request>0 and a.id1=b.id1 and a.id2=b.id2;
(select username from v$session where sid=a.sid) blocker,
a.sid,’ is blocking ‘,
(select username from v$session where sid=b.sid) blockee,
b.sid
from v$lock a,v$lock b
where a.block=1 and b.request>0 and a.id1=b.id1 and a.id2=b.id2;
结果如下:
BLOCKER SID ‘ISBLOCKING’ BLOCKEE SID
—————————— ———- ————- —————————— ———-
LIHUILIN 14 is blocking LIHUILIN 139
—————————— ———- ————- —————————— ———-
LIHUILIN 14 is blocking LIHUILIN 139
Kill引起阻塞的Session
select ‘alter system kill session ”’||sid||’,’||serial#||”’;’ cmd from v$session where username=’LIHUILIN’ and sid=14;
结果如下:
CMD
—————————————–
alter system kill session ‘14,87’;
—————————————–
alter system kill session ‘14,87’;
最后执行alter system命令,阻塞解除。