Oracle KILL 会话常用方法

通常会用ALTER SYSTEM KILL SESSION 'sid,serial#'杀掉指定会话,但有时会发现执行后只是将会话状态为KILLED,依然会阻塞其它会话,数据库也不会立即释放相关资源。网上查资料Oracle KILL 会话有三种方式,下面分别来看:

 

一、 ALTER SYSTEM KILL SESSION

alter system kill session实际上不是真正的杀死会话,它只是将会话标记为KILLED,并将其addr(v$session.addr字段可以看到)指向一个虚拟地址,切断客户端与Server Process的映射关联,此时Server Process并没有回收,等待PMON进程来清除会话。所以,等待一个被标记为Killed的Session退出可能需要花费很长的时间。

如果此时被Kill的会话重新尝试执行任务,马上会收到进程中断的提示,此时Oracle会立即启动PMON来清除该session,旧的Server Process才被回收释放掉,这被作为一次异常中断处理。

可以使用IMMEDIATE关键字 回滚事务、释放会话持有的锁、并立即返回当前会话的控制权。

ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;
-- kill指定用户非活跃会话
select 'alter system kill session '''||s.sid||','||s.SERIAL#||''' immediate;' from v$session s where s.status='INACTIVE' and s.USERNAME= upper('xxx');

官方文档说明如下:

The KILL SESSION clause lets you mark a session as terminated, rollback ongoing transactions, release all session locks, and partially recover session resources. To use this clause, your instance must have the database open. 

If the session is performing some activity that must be completed, such as waiting for a reply from a remote database or rolling back a transaction, then Oracle Database waits for this activity to complete, marks the session as terminated, and then returns control to you. If the waiting lasts a minute, then Oracle Database marks the session to be terminated and returns control to you with a message that the session is marked to be terminated. The PMON background process then marks the session as terminated when the activity is complete.

Specify IMMEDIATE to instruct Oracle Database to roll backongoing transactions, release all session locks, recover the entire session state, and return control to you immediately.

 

二、 ALTER SYSTEM DISCONNECT SESSION

ALTER SYSTEM DISCONNECT SESSION 等价于从操作系统杀掉进程。有两个选项:POST_TRANSACTION和IMMEDIATE;POST_TRANSACTION表示等待事务完成后断开会话,IMMEDIATE表示中断会话,立即回滚事务。

ALTER SYSTEM DISCONNECT SESSION 'sid,serial#' POST_TRANSACTION;
ALTER SYSTEM DISCONNECT SESSION 'sid,serial#' IMMEDIATE;

 

三、操作系统KILL进程

可以使用下面SQL语句找到对应的操作系统进程SPID,在执行前,一定要谨慎确认。

select SPID from v$process where addr in (SELECT PADDR FROM V$SESSION WHERE USERNAME='xxx');
-- 或者
SET LINESIZE 100
COLUMN spid FORMAT A10
COLUMN username FORMAT A10
COLUMN program FORMAT A45
 
SELECT s.inst_id,
       s.sid,
       s.serial#,
       p.spid,
       s.username,
       s.program
FROM   gv$session s
       JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id
WHERE  s.type != 'BACKGROUND';

操作系统执行

#Linux
KILL -9 SPID

#Windows
orakill ORACLE_SID spid

 

四、 如何kill状态为killed的进程

有时执行alter system kill session(包括带IMMEDIATE关键字)后会发现会话一直没有在回滚,只是状态变为killed,还是在阻塞别的会话,这时应该怎么找到对应os pid把它彻底kill掉?

select sid,serial# from v$session where username='SCOTT';
       SID    SERIAL#
---------- ----------
        35         51

alter system kill session '35,51';

select status,program,server from v$session where username='SCOTT';

STATUS   PROGRAM                                          SERVER
-------- ------------------------------------------------ ---------
KILLED   sqlplus.exe                                      PSEUDO
  • 11g之前
-- 注意不要kill D000的进程,也可以再加些过滤条件
select spid,program from v$process
where program!= 'PSEUDO'
and addr not in (select paddr from v$session)
and addr not in (select paddr from v$bgprocess)
and addr not in (select paddr from v$shared_server);

SPID                     PROGRAM
------------------------ ------------------------------------------------
9659                     oracle@orcl9i
7095                     oracle@orcl9i (D000)
  • 11g开始变简单了很多
select spid,program from v$process where addr in (select creator_addr from v$session where status='KILLED');

SPID                     PROGRAM
------------------------ ------------------------------------------------
9659                     oracle@orcl9i

再在os级kill掉即可

 

五、 总结

要在数据库彻底杀掉一个会话,尤其是大事务会话,最好是使用ALTER SYSTEM DISCONNECT SESSION IMMEDIATE或使用下面步骤(反之亦可):

  • 在操作系统级别Kill掉进程。
  • 在数据库内部KILL SESSION

 

参考

https://www.cnblogs.com/andy6/p/5778363.html

https://www.cnblogs.com/kerrycode/p/4034231.html

https://www.eygle.com/faq/Kill_Session.htm

https://yq.aliyun.com/articles/458451/

http://blog.itpub.net/17203031/viewspace-683786/

http://blog.itpub.net/17203031/viewspace-684039/

你可能感兴趣的:(脚本&命令,Oracle)