跳到主要内容

锁等待时间过长处理

ora-04021

解释是等待锁定对象时发生超时

死锁

首先查看是否在表上发生了死锁

select object_name,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;

查询导致锁表的sql

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

kill掉锁表进程

alter system kill session '<sid>,<serial#>';

kill提示ORA-00031 marked for kill,查询程序进程ID,在系统层面杀死进程

select spid,osuser,s.program from v$session s,v$process p where s.paddr=p.addr and s.sid=<sid>;
#Linux为例
kill -9 <spid>

正在执行的语句

如果没有发生死锁,优先考虑是否有进程占用或挂起的情况,查询正在执行的sql语句

SELECT 
s.sid,
s.serial#,
s.username,
t.sql_text
FROM
v$session s,
v$sqltext_with_newlines t
WHERE
t.address = s.sql_address
AND t.hash_value = s.sql_hash_value
AND s.status = 'ACTIVE';

kill掉进程

alter system kill session '<sid>,<serial#>';