今天碰到一個(gè)問題,有一張表不能操作,很可能是被鎖了,首先想到的是kill session,于是執(zhí)行了下列的腳本找到是哪個(gè)session有問題:
查看表是否被鎖
SELECT /*+ rule*/
a.sid, b.owner, object_name, object_type
FROM v$lock a, all_objects b
WHERE TYPE = 'TM'
and a.id1 = b.object_id;
根據(jù)上面查詢出的sid,找出對(duì)應(yīng)的serial#:
SELECT sid,serial# FROM v$session WHERE sid = sid;
發(fā)現(xiàn)有一個(gè)會(huì)話有鎖sid 197,serial# 17,于是執(zhí)行alter system kill session ‘197,17';大概等了30s中,pl/sql developer報(bào)出一個(gè)錯(cuò)誤:ora-00031:標(biāo)記要終止的會(huì)話。
解決方法:查出session的spid
select spid, osuser, s.program from v$session s, v$process p where s.paddr = p.addr and s.sid =197;
1. 在linux上, kill -9 12345
2. 在windows上,C:\Documents and Settings\gg>orakill orcl 12345
orcl:表示要?dú)⑺赖倪M(jìn)程屬于的實(shí)例名
12345:是要?dú)⒌舻木€程號(hào)
ORA-00031: session marked for kill
Cause: The session specified in an ALTER SYSTEM KILL SESSION command cannot bekilled immediately (because it is rolling back or blocked on a networkoperation), but it has been marked for kill. This means it will be killed as soonas possible after its current uninterruptible operation is done.
Action: No action is required for the session to be killed, but further executionsof the ALTER SYSTEM KILL SESSION command on this session may cause the sessionto be killed sooner.
下面是補(bǔ)充:
oracle中編譯一個(gè)包的時(shí)候發(fā)現(xiàn),只要一編譯就卡死了,后來使用《oracle查詢、Kill鎖資源的SQL語(yǔ)句》方法查鎖的時(shí)候發(fā)現(xiàn)包中用到的一些資源一直處于死鎖狀態(tài)。后來通過alter system kill session的方法去解鎖,卻發(fā)現(xiàn)出現(xiàn)如下圖1所示的問題:
ORA-00031:標(biāo)記要終止的會(huì)話
ORA-00031:標(biāo)記要終止的會(huì)話
后來通過連接上oracle數(shù)據(jù)庫(kù)所在的系統(tǒng),然后kill掉進(jìn)程解決,方法如下:
(1)查詢被鎖資源的sid、serial#:
SELECT s.sid,
s.serial#,
v.*,
ao.*
FROM v$locked_object v,
all_objects ao,
v$session s
WHERE v.object_id = ao.object_id
AND s.sid = v.session_id;
(2)利用上面的SQL查詢出sid和serial#以后,利用下面SQL去kill session:
-- 如有記錄則表示lock,記錄下SID和serial#,將記錄下的SID和serial#替換下面的738,1429,即可接觸lock。
ALTER SYSTEM KILL SESSION '738,1429';
(3)如果上面的kill session報(bào)如上圖1的錯(cuò)誤,那么再用下面的SQL去查處session對(duì)應(yīng)的spid:
SELECT p.spid,
s.osuser,
s.program
FROM v$session s,
v$process p
WHERE s.paddr = p.addr
AND s.sid = 37; -- 替換成相應(yīng)的session_id
(4)連上oracle數(shù)據(jù)庫(kù)所在的系統(tǒng)(我這里是Linux系統(tǒng)),記錄下上面查到的spid(假設(shè)這里查到的是1133),然后使用下面語(yǔ)句來kill掉進(jìn)程:
kill -9 1133
等待一會(huì),等進(jìn)程kill成功以后,再去查鎖即可發(fā)現(xiàn)資源已經(jīng)被釋放。如果oracle安裝在Windows系統(tǒng)上,請(qǐng)使用Windows系統(tǒng)的kill進(jìn)程的方式去殺掉進(jìn)程。
您可能感興趣的文章:- Oracle中查看引起Session阻塞的2個(gè)腳本分享
- Oracle中的Connect/session和process的區(qū)別及關(guān)系介紹
- ORACLE 如何查詢被鎖定表及如何解鎖釋放session
- 對(duì)于oracle對(duì)session進(jìn)行跟蹤的分析