Oracle:完全結束會話 ,完全解鎖

oracle會話被鎖是常常的。但有時alter system kill session 'sid,serial#';並不能完全的殺死會話。只能經過殺死Linux上對應的進程才行。
之前都是經過v$session裏的logon_time,和ps -ef|grep oracle所列出的時間大約的定位進程。而後結束。原本想把這個寫成日誌。但有一個服務器存在了好幾個前幾天啓動的進程(估計是我kill -9 weblogic進程產生的) ps -ef不能只能列出進程啓動的日期,不能列出具體時間。
上網查到了2篇詳細的解決方法。就轉了過來:
Oracle殺死死鎖進程
先查看哪些表被鎖住了:
       
       
       
       select b.owner,b.object_name,a.session_id,a.locked_mode
from v$locked_object a,dba_objects b
where b.object_id = a.object_id;
OWNER        OBJECT_NAME        SESSION_ID LOCKED_MODE
------------------------------ -----------------
WSSB SBDA_PSHPFTDT      22 3
WSSB_RTREPOS WB_RT_SERVICE_QUEUE_TAB      24 2
WSSB_RTREPOS WB_RT_NOTIFY_QUEUE_TAB      29 2
WSSB_RTREPOS WB_RT_NOTIFY_QUEUE_TAB      39 2
WSSB SBDA_PSDBDT        47 3
WSSB_RTREPOS WB_RT_AUDIT_DETAIL        47 3
select b.username,b.sid,b.serial#,logon_time 
from v$locked_object a,v$session b
where a.session_id = b.sid order by b.logon_time;
USERNAME      SID      SERIAL# LOGON_TIME
------------------------------ ---------- -------
WSSB_RTACCESS        39        1178 2006-5-22 1
WSSB_RTACCESS        29        5497 2006-5-22 1
殺進程中的會話:
       
       
       
       alter system kill session 'sid,serial#';
e.g
alter system kill session '29,5497';
若是有ora-00031錯誤,則在後面加immediate;alter system kill session '29,5497' immediate;
如何殺死oracle死鎖進程
1.查哪一個過程被鎖:
查V$DB_OBJECT_CACHE視圖:
SELECT * FROM V$DB_OBJECT_CACHE WHERE OWNER='過程的所屬用戶' AND CLOCKS!='0';
2. 查是哪個SID,經過SID可知道是哪一個SESSION:
查V$ACCESS視圖:
SELECT * FROM V$ACCESS WHERE OWNER='過程的所屬用戶' AND NAME='剛纔查到的過程名';
3. 查出SID和SERIAL#:
查V$SESSION視圖:
SELECT SID,SERIAL#,PADDR FROM V$SESSION WHERE SID='剛纔查到的SID';
V$PROCESS視圖:
SELECT SPID FROM V$PROCESS WHERE ADDR='剛纔查到的PADDR';
4. 殺進程:
(1)先殺ORACLE進程:
ALTER SYSTEM KILL SESSION '查出的SID,查出的SERIAL#';
(2)再殺操做系統進程:
KILL -9 剛纔查出的SPID或ORAKILL 剛纔查出的SID 剛纔查出的SPID。
Oracle的死鎖
查詢數據庫死鎖:
       
       
       
       select t2.username||'      '||t2.sid||'     
 '||t2.serial#||'      '||t2.logon_time||'     
 '||t3.sql_text
from v$locked_object t1,v$session t2,v$sqltext t3
where t1.session_id=t2.sid 
and t2.sql_address=t3.address
order by t2.logon_time;
查詢出來的結果就是有死鎖的session了,下面就是殺掉,拿到上面查詢出來的SID和SERIAL#,填入到下面的語句中:
alter system kill session 'sid,serial#';
通常狀況能夠解決數據庫存在的死鎖了,或經過session id 查到對應的操做系統進程,在Unix中殺掉操做系統的進程。
       
       
       
       SELECT a.username,c.spid AS os_process_id,c.pid 
AS oracle_process_id FROM v$session a,v$process c 
WHERE c.addr=a.paddr and a.sid= and a.serial#= ;
而後採用kill (unix) 或 orakill(windows )。
在Unix中:
       
       
       
       ps -ef|grep os_process_id
kill -9 os_process_id
ps -ef|grep os_process_id
常常在Oracle的使用過程當中碰到這個問題,因此也總結了一點解決方法。
1)查找死鎖的進程:
       
       
       
       sqlplus "/as sysdba"      (sys/change_on_install)
SELECT s.username,l.OBJECT_ID,l.SESSION_ID,s.SERIAL#,
l.ORACLE_USERNAME,l.OS_USER_NAME,l.PROCESS 
FROM V$LOCKED_OBJECT l,V$SESSION S WHERE l.SESSION_ID=S.SID;
2)kill掉這個死鎖的進程:
alter system kill session ‘sid,serial#’; (其中sid=l.session_id)
3)若是還不能解決:
       
       
       
       select pro.spid from v$session ses,
v$process pro where ses.sid=XX and 
ses.paddr=pro.addr;
其中sid用死鎖的sid替換:
       
       
       
       exit
ps -ef|grep spid
其中spid是這個進程的進程號,kill掉這個Oracle進程。
 
----------------------------------------------------------------------------------------------------------
 
咱們知道,在Oracle數據庫中,能夠經過kill session的方式來終止一個進程,其基本語法結構爲:
alter system kill session '''' sid,serial#'''' ;
被kill掉的session,狀態會被標記爲killed,Oracle會在該用戶下一次touch時清除該進程.

咱們發現當一個session被kill掉之後, lag管:\u業網育a網供M該session的paddr被修改,若是有多個session被kill,那麼多個session
的paddr都被更改成相同的進程地址:
SQL> select saddr,sid,serial#,paddr,username,status from v$session where username is not null;

SADDR              SID       SERIAL# PADDR       USERNAME                          STATUS
-------- ---------- ---------- -------- ------------------------------ --------
542E0E6C            11           314 542B70E8 EYGLE                             INACTIVE
542E5044            18           662 542B6D38 SYS                               ACTIVE


SQL> alter system kill session ''''11,314'''';

System altered.

SQL> select saddr,sid,serial#,paddr,username,status from v$session where username is not null;

SADDR              SID       SERIAL# PADDR       USERNAME                          STATUS
-------- ---------- ---------- -------- ------------------------------ --------
542E0E6C            11           314 542D6BD4 EYGLE                             KILLED
542E5044            18           662 542B6D38 SYS                               ACTIVE


SQL> select saddr,sid,serial#,paddr,username,status from v$session where username is not null;

SADDR              SID       SERIAL# PADDR       USERNAME                          STATUS
-------- ---------- ---------- -------- ------------------------------ --------
542E0E6C            11           314 542D6BD4 EYGLE                             KILLED
542E2AA4            14           397 542B7498 EQSP                              INACTIVE
542E5044            18           662 542B6D38 SYS                               ACTIVE

SQL> alter system kill session ''''14,397'''';

System altered.

SQL> select saddr,sid,serial#,paddr,username,status from v$session where username is not null;

SADDR              SID       SERIAL# PADDR       USERNAME                          STATUS
-------- ---------- ---------- -------- ------------------------------ --------
542E0E6C            11           314 542D6BD4 EYGLE                             KILLED
542E2AA4            14           397 542D6BD4 EQSP                              KILLED
542E5044            18           662 542B6D38 SYS                               ACTIVE

在這種狀況下,不少時候, c_件D?L~?\%j3Sd資源是沒法釋放的,咱們須要查詢spid,在操做系統級來kill這些進程.
可是因爲此時v$session.paddr已經改變,咱們沒法經過v$session和v$process關聯來得到spid
那還能夠怎麼辦呢?
咱們來看一下下面的查詢:
     SQL> SELECT s.username,s.status,
     2     x.ADDR,x.KSLLAPSC,x.KSLLAPSN,x.KSLLASPO,x.KSLLID1R,x.KSLLRTYP,
     3     decode(bitand (x.ksuprflg,2),0,null,1)
     4     FROM x$ksupr x,v$session s
     5     WHERE s.paddr(+)=x.addr
     6     and bitand(ksspaflg,1)!=0;


USERNAME                          STATUS      ADDR          KSLLAPSC      KSLLAPSN KSLLASPO          KSLLID1R KS D
------------------------------ -------- -------- ---------- ---------- ------------ ---------- -- -
                                           542B44A8             0             0                          0
                                  ACTIVE      542B4858             1            14 24069                    0       1
                                  ACTIVE      542B4C08            26            16 15901                    0       1
                                  ACTIVE      542B4FB8             7            46 24083                    0       1
                                  ACTIVE      542B5368            12            15 24081                    0       1
                                  ACTIVE      542B5718            15            46 24083                    0       1
                                  ACTIVE      542B5AC8            79             4 15923                    0       1
                                  ACTIVE      542B5E78            50            16 24085                    0       1
                                  ACTIVE      542B6228           754            15 24081                    0       1
                                  ACTIVE      542B65D8             1            14 24069                    0       1
                                  ACTIVE      542B6988             2            30 14571                    0       1

USERNAME                          STATUS      ADDR          KSLLAPSC      KSLLAPSN KSLLASPO          KSLLID1R KS D
------------------------------ -------- -------- ---------- ---------- ------------ ---------- -- -
SYS                               ACTIVE      542B6D38             2             8 24071                    0
                                           542B70E8             1            15 24081                  195 EV
                                           542B7498             1            15 24081                  195 EV

SYS                               INACTIVE 542B7848             0             0                          0
SYS                               INACTIVE 542B7BF8             1            15 24081                  195 EV

16 rows selected.
咱們注意,紅字標出的部分就是被Kill掉的進程的進程地址.
簡化一點,其實就是以下概念:
SQL> select p.addr from v$process p where pid <> 1
2 minus
3 select s.paddr from v$session s;
ADDR
--------
542B70E8
542B7498
Ok, OY[)Yj?=的專軟
ykGo管垠y國的n專u網k無D
如今咱們得到了進程地址,就能夠在v$process中找到spid,而後可使用Kill或者orakill在系統級來殺掉這些進程.
實際上,我猜想:
當在Oracle中kill session之後, Oracle只是簡單的把相關session的paddr 指向同一個虛擬地址.
此時v$process和v$session失去關聯,進程就此中斷.
而後Oracle就等待PMON去清除這些Session.因此一般等待一個被標記爲Killed的Session退出須要花費很長的時間.
若是此時被Kill的process,從新嘗試執行任務,那麼立刻會收到進程中斷的提示,process退出,此時Oracle會當即啓動PMON 來清除該session.這被做爲一次異常中斷處理.
相關文章
相關標籤/搜索