kill session 是DBA经常碰到的事情之一。如果kill 掉了不该kill 的session,则具有破坏性,因此尽可能的避免这样的错误发生。同时也应当注意,
如果kill 的session属于oracle 后台进程,则容易导致数据库实例宕机。
通常情况下,并不需要从操作系统级别杀掉Oracle会话进程,但并非总是如此,下面的描述中给出了在Oracle级别杀掉会话以及操作系统级别杀掉进程。
 
 
一、获得需要kill session的信息(使用V$SESSION 和 GV$SESSION视图)
 
  SET LINESIZE 180
  COLUMN spid FORMAT A10
  COLUMN username FORMAT A10
  COLUMN program FORMAT A40
 
  SELECT s.inst_id,
         s.sid,
         s.serial#,
         p.spid,
         s.username,
         s.program,
         s.paddr,
         s.STATUS
  FROM   gv$session s
         JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id
  WHERE  s.type != 'BACKGROUND';
 
     INST_ID        SID    SERIAL# SPID       USERNAME   PROGRAM                                       PADDR    STATUS
  ---------- ---------- ---------- ---------- ---------- --------------------------------------------- -------- --------
           1        146         23 27573      TEST       sqlplus@oracle10g (TNS V1-V3)                 4C621950 INACTIVE
           1        160         17 27610      SYS        sqlplus@oracle10g (TNS V1-V3)                 4C624174 ACTIVE
           1        144         42 27641      SCOTT      sqlplus@oracle10g (TNS V1-V3)                 4C624730 INACTIVE
        
二、使用ALTER SYSTEM KILL SESSION 命令实现
  语法:
      SQL> ALTER SYSTEM KILL SESSION 'sid,serial#';
      SQL> ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;    
SQL Language Reference 
里对Immediate的解释是:IMMEDIATE Specify IMMEDIATE to instruct Oracle
Database to roll back ongoing transactions, release all session locks, recover the entire session state,
and return control to you immediately.
文章最后有oracle官方文档给出的杀进程文档详细解释

对于RAC环境下的kill session ,需要搞清楚需要kill 的session 位于哪个节点,可以查询GV$SESSION视图获得。
    kill session 的时候仅仅是将会话杀掉。在有些时候,由于较大的事务或需要运行较长的SQL语句将导致需要kill的session并不能立即杀掉。对于这种情
    况将收到 "marked for kill"提示(如下),一旦会话当前事务或操作完成,该会话被立即杀掉。
   
    alter system kill session '4730,39171'
    *
    ERROR at line 1:
    ORA-00031: session marked for kill
 
 
  在下面的操作中将杀掉会话146,144
    sys@AUSTIN> alter system kill session '146,23';
   
    System altered.
   
    sys@AUSTIN> alter system kill session '144,42';
   
    System altered.
   
    sys@AUSTIN> select inst_id,saddr,sid,serial#,paddr,username,status,program from gv$session where username is not null;
   
       INST_ID SADDR           SID    SERIAL# PADDR    USERNAME   STATUS   PROGRAM
    ---------- -------- ---------- ---------- -------- ---------- -------- ---------------------------------------------
             1 4C70BF04        144         42 4C6545A0 SCOTT      KILLED   sqlplus@oracle10g (TNS V1-V3)
             1 4C70E6B4        146         23 4C6545A0 TEST       KILLED   sqlplus@oracle10g (TNS V1-V3)
             1 4C71FC84        160         17 4C624174 SYS        ACTIVE   sqlplus@oracle10g (TNS V1-V3)
                 
     注意:在查询中可以看到被杀掉的会话的PADDR地址发生了变化,参照查询结果中的红色字体。如果多个session被kill 掉,则多个session的PADDR
     被改为相同的进程地址。
 
  通过下面的语句来找回被kill 掉的ADDR先前的地址
    SELECT s.username,s.status,
    x.ADDR,x.KSLLAPSC,x.KSLLAPSN,x.KSLLASPO,x.KSLLID1R,x.KSLLRTYP,
    decode(bitand (x.ksuprflg,2),0,null,1)
    FROM x$ksupr x,v$session s
    WHERE s.paddr(+)=x.addr
    and bitand(ksspaflg,1)!=0;      
   
    USERNAME   STATUS   ADDR       KSLLAPSC   KSLLAPSN KSLLASPO       KSLLID1R KS D
    ---------- -------- -------- ---------- ---------- ------------ ---------- -- -
               ACTIVE   4C623BB8         99          4 27468               275 EV 1
               ACTIVE   4C623040          9         24 27444                 0    1
               ACTIVE   4C622A84        101          4 27480               274 EV 1
               ACTIVE   4C6224C8          1         48 27450                 0    1
               ACTIVE   4C621F0C          1         48 27450                 0    1
               ACTIVE   4C6235FC          2          4 27468                 0    1
    SYS        ACTIVE   4C624174          2         15 27442                 0
               ACTIVE   4C62081C          1         48 27440                 0    1
               ACTIVE   4C621394          1         48 27440                 0    1
               ACTIVE   4C620DD8         11         24 27476                 0    1
               ACTIVE   4C61F6E8         15          4 27610                 0    1
               ACTIVE   4C620260        222         24 27450                 0    1
               ACTIVE   4C61FCA4          7         25 27573                 0    1
               ACTIVE   4C61F12C          6         25 27573                 0    1
               ACTIVE   4C61EB70          4         24 27458                 0    1
               ACTIVE   4C61E5B4          1         48 27440                 0    1
               ACTIVE   4C61DFF8          2         24 27444                 0    1
                        4C624730          0          0                       0
                        4C621950          0          0                       0
                        4C61DA3C          0          0                       0
                   
 
  或者根据下面的语句来获得发生变化的addr
    sys@AUSTIN> select p.addr from v$process p where pid <> 1
      2  minus
      3  select s.paddr from v$session s;
   
    ADDR
    --------
    4C621950
    4C624730                   
 
三、在操作系统级别杀掉会话
  寻找会话对应的操作系统的进程ID
    sys@AUSTIN> select SPID from  v$process where ADDR in ('4C621950','4C624730') ;        
   
    SPID
    ----------
    27573
    27641
   
  使用kill 命令来杀掉操作系统级别进程ID
    kill session -9 27573
   
    kill session -9 27641
 
四、获得当前会话的SID
  SQL> select userenv('sid') from dual;
 
  USERENV('SID')
  --------------
             627
 
五、多个会话需要kill 的处理办法
  1.根据给定的SID(用户名)查找需要杀掉会话的信息,包括位于哪一个实例
    set linesize 160
    col program format a35
    col username format a18
    select inst_id,saddr,sid,serial#,paddr,username,status,program from gv$session
    where sid in ('2731','2734','2720','2678','2685')
    and username='CTICUST'
    order by inst_id;
   
       INST_ID SADDR                   SID    SERIAL# PADDR            USERNAME           STATUS   PROGRAM
    ---------- ---------------- ---------- ---------- ---------------- ------------------ -------- ---------------------------
             1 00000003DAF8F870       2678       8265 00000003DBC6CA08 MSS4USR            INACTIVE JDBC Thin Client
             1 00000003DAF98E48       2685         83 00000003DBC08510 MSS4USR            ACTIVE   JDBC Thin Client
             1 00000003DAFC7B80       2720          5 00000003DBBEDA20 MSS4USR            INACTIVE JDBC Thin Client
             1 00000003DAFD66F8       2731          3 00000003DBBE9AE0 SYS                ACTIVE  racgimon@svdg0028(TNS V1-V3)
             1 00000003DAFDA730       2734         15 00000003DBBEC268 MSS4USR            INACTIVE JDBC Thin Client
             2 00000003DAFD66F8       2731          1 00000003DBBE92F8                    ACTIVE   oracle@svdg0029 (ARC0)
   
    上面的查询中有一个SID为2731的位于节点2上。
    也可以通过下面的方式来获得RAC的节点信息,便于确定需要kill 的session究竟位于哪一个节点。
   
      set linesize 160
      col HOST_NAME format a25
      SQL> select INSTANCE_NUMBER,INSTANCE_NAME,HOST_NAME,VERSION,STATUS from gv$instance order by 1;
 
      INSTANCE_NUMBER INSTANCE_NAME    HOST_NAME                 VERSION           STATUS
      --------------- ---------------- ------------------------- ----------------- ------------
                    1 O02WMT1A         svd0051                  10.2.0.4.0        OPEN
                    2 O02WMT1B         svd0052                  10.2.0.4.0        OPEN
                    3 O02WMT1C         svd0053                  10.2.0.4.0        OPEN
                   
  2.使用下面查询来生成kill session 的语句
    select 'alter system kill session '''|| sid ||',' ||SERIAL# ||''''||';'  from  gv$session
    where sid in ('2731','2734','2720','2678','2685')
    order by inst_id;
     
    获得下列kill session的语句,根据要求由于此次需要杀掉的session全部位于节点1,因此登录到节点节点1执行下面的语句  
        
    alter system kill session '2678,8265';
   
    alter system kill session '2685,83';
   
    alter system kill session '2720,5'; 
   
    alter system kill session '2731,3';
    
    alter system kill session '2734,15';
    
    alter system kill session '2731,1';    --此条命令不需要执行,该session位于节点2。

ps.

Terminating Sessions

Sometimes it is necessary to terminate current user sessions. For example, you might want to perform an administrative operation and need to terminate all non-administrative sessions. This section describes the various aspects of terminating sessions, and contains the following topics:

When a session is terminated, any active transactions of the session are rolled back, and resources held by the session (such as locks and memory areas) are immediately released and available to other sessions.

You terminate a current session using the SQL statement ALTER SYSTEM KILL SESSION. The following statement terminates the session whose system identifier is 7 and serial number is 15:

ALTER SYSTEM KILL SESSION '7,15';

Identifying Which Session to Terminate

To identify which session to terminate, specify the session index number and serial number. To identify the system identifier (SID) and serial number of a session, query the V$SESSION dynamic performance view. For example, the following query identifies all sessions for the user jward:

SELECT SID, SERIAL#, STATUS
FROM V$SESSION
WHERE USERNAME = 'JWARD'; SID SERIAL# STATUS
----- --------- --------
7 15 ACTIVE
12 63 INACTIVE

A session is ACTIVE when it is making a SQL call to Oracle Database. A session is INACTIVE if it is not making a SQL call to the database.

See Also:

Oracle Database Reference for a description of the status values for a session

Terminating an Active Session

If a user session is processing a transaction (ACTIVE status) when you terminate the session, the transaction is rolled back and the user immediately receives the following message:

ORA-00028: your session has been killed

If, after receiving the ORA-00028 message, a user submits additional statements before reconnecting to the database, Oracle Database returns the following message:

ORA-01012: not logged on

An active session cannot be interrupted when it is performing network I/O or rolling back a transaction. Such a session cannot be terminated until the operation completes. In this case, the session holds all resources until it is terminated. Additionally, the session that issues the ALTER SYSTEM statement to terminate a session waits up to 60 seconds for the session to be terminated. If the operation that cannot be interrupted continues past one minute, the issuer of the ALTER SYSTEM statement receives a message indicating that the session has been marked to be terminated. A session marked to be terminated is indicated in V$SESSION with a status of KILLED and a server that is something other than PSEUDO.

Terminating an Inactive Session

If the session is not making a SQL call to Oracle Database (is INACTIVE) when it is terminated, the ORA-00028 message is not returned immediately. The message is not returned until the user subsequently attempts to use the terminated session.

When an inactive session has been terminated, the STATUS of the session in the V$SESSION view is KILLED. The row for the terminated session is removed from V$SESSION after the user attempts to use the session again and receives the ORA-00028 message.

In the following example, an inactive session is terminated. First, V$SESSION is queried to identify the SID and SERIAL# of the session, and then the session is terminated.

SELECT SID,SERIAL#,STATUS,SERVER
FROM V$SESSION
WHERE USERNAME = 'JWARD'; SID SERIAL# STATUS SERVER
----- -------- --------- ---------
7 15 INACTIVE DEDICATED
12 63 INACTIVE DEDICATED
2 rows selected. ALTER SYSTEM KILL SESSION '7,15';
Statement processed. SELECT SID, SERIAL#, STATUS, SERVER
FROM V$SESSION
WHERE USERNAME = 'JWARD'; SID SERIAL# STATUS SERVER
----- -------- --------- ---------
7 15 KILLED PSEUDO
12 63 INACTIVE DEDICATED
2 rows selected.

转载:http://blog.csdn.net/leshami/article/details/6439019

Oracle彻底杀掉进程的更多相关文章

  1. kill session真的能杀掉进程吗

    session1 确认sidSYS @ prod > select userenv('sid') from dual; USERENV('SID')-------------- 144 sess ...

  2. Linux下使用ps命令来查看Oracle相关的进程

    Linux下可以使用ps命令来查看Oracle相关的进程 Oracle Listener 这个命令会列出Oracle Net Listener的进程 [oracle@ www.linuxidc.com ...

  3. Oracle删除死锁进程的方法

    本文实例讲述了Oracle删除死锁进程的方法.分享给大家供大家参考.具体如下: 步骤1:用以下SQL查看进程列表,判断出被锁定的表 复制代码代码如下: SELECT dob.OBJECT_NAME T ...

  4. Oracle体系结构之进程

    Oracle体系结构之进程 一.概述 Oracle中的每个进程都要执行一个特定的任务(或者一组任务),每个进程都会为自己分配内存(PGA)来完成它的任务.一个Oracle实例主要有以下3类进程: (1 ...

  5. centos7 nginx安装/启动/进程状态/杀掉进程

    1.安装     下载RPM:wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.10.0-1.el7.ngx.x86_64.rpm ...

  6. oracle 11g杀掉锁的sql

    oracle 11g杀掉锁的sql [引用 2013-3-6 17:19:12]     字号:大 中 小 --查询出出现锁的session_idselect session_id from v$lo ...

  7. C#杀掉进程的方法

    C#杀掉进程的方法 private static string CmdName = "cmd"; /// <summary> /// 关闭进程 /// </sum ...

  8. Linux下可以使用ps命令来查看Oracle相关的进程

    Linux下可以使用ps命令来查看Oracle相关的进程 Oracle Listener 这个命令会列出Oracle Net Listener的进程 [oracle@ www.linuxidc.com ...

  9. linux按照进程名杀掉进程

    1.按照进程名杀掉进程 ps  -ef  | grep   sftp   | grep mysql  |grep -v grep | awk '{print("kill -9 ", ...

随机推荐

  1. POJ - 1222 / POJ - 3279 枚举第一行

    说好的高斯消元法呢,暴搜都能0ms 这种翻转就是枚举第一行控制变量下面行就全都确定了 代码参考挑战程序设计例题 #include<iostream> #include<algorit ...

  2. C++ GUI Qt4 编程 (第二版)

    [加拿大]JasminBlanchette [英]MarkSummerfield . 电子工业 2008. 前几天的问题多是因为版本不兼容的问题. QT本身Q4 Q5就有版本问题,然后集成到VS08 ...

  3. asp.net mvc 静态化

    静态化的基本理解就是,常用的资源以文本形式保存,客户端访问时无需经过程序处理,直接下载 但是不存在的文件需要经过程序处理,文件内容如果需要有更动或删除,则直接删除文件本身 1.IIS Express ...

  4. Oracle PLSQL INDEX BY Binary_Integer 测试

    [转自] http://blog.chinaunix.net/uid-14669803-id-2921539.html DECLARE TYPE t_list_1 IS TABLE OF VARCHA ...

  5. (转)求有向图的强连通分量个数(kosaraju算法)

    有向图的连通分量的求解思路 kosaraju算法 逛了很多博客,感觉都很难懂,终于找到一篇能看懂的,摘要记录一下 原博客https://www.cnblogs.com/nullzx/p/6437926 ...

  6. 「BZOJ1485」[HNOI2009] 有趣的数列 (卡特兰数列)

    「BZOJ1485」[HNOI2009] 有趣的数列   Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai ...

  7. 关于i18n

    现在工作主要负责小程序端,很少负责backend.最近的一个任务是配置多语言.因为一开始都是写死的中文,现在需要把那些变成英文. 狂搜了一波,其实网上的方法都不怎好.(可能就是一开始看的时候觉得好.) ...

  8. (转)增加定时检测linux占用内存,及时清理功能

    增加定时检测linux占用内存,及时清理功能 原文:http://www.voidcn.com/article/p-wnmannom-boa.html free -m 查看,发现内存跑满了. 再 to ...

  9. LeetCode 200.岛屿的个数

    给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...

  10. net.sf.json.JSONException: There is a cycle in the hierarchy! 转json死循环问题解决

    解决上述问题遵照两个原则就可以: 1.页面不需要展示关联数据时 解决:将关联对象属性排除掉 2.页面需要展示关联数据时 解决:将关联对象改为立即加载,并且将关联对象中的属性排除