zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Oracle 杀死INACTIVE,SNIPED会话

Oracle 会话 杀死
2023-09-14 09:01:48 时间

 

下面是模拟出产生SNIPED会话,以及如何杀死会话避免生产环境连接数超过最大连接数,同理杀死INACTIVE会话原理一样。

 

(1)现在在主机database1上面连接远程database2上面的库,使用test用户登入。

[oracle@Database1 dbs]$ tnsping database2

 

TNS Ping Utility for Linux: Version 11.2.0.4.0 - Production on 24-MAY-2018 19:09:18

 

Copyright (c) 1997, 2013, Oracle.  All rights reserved.

 

Used parameter files:

 

 

Used TNSNAMES adapter to resolve the alias

Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.57.129)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = oradb)))

OK (30 msec)

[oracle@Database1 dbs]$ sqlplus test/test

 

SQL*Plus: Release 11.2.0.4.0 Production on Thu May 24 19:09:28 2018

 

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

 

 

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

 

SQL>

 

(2)到主机database2上面查看具体的test用户信息,test用户状态是SNIPED的。

SQL> col username for a20;

SQL>select SID,USERNAME,STATUS from v$session;

  SID    USERNAME  STATUS

---------- -------------------- ----------------

41       TEST  SNIPED

 

 

下面这个语句可以更加清楚的看到连接的信息,v$session和v$process结合起来。。

SQL> col SPID for a30;

SQL> col PROGRAM for a30;

SQL> col MACHINE for a30;

SQL> select  b.spid,a.sid,a.username,a.program,a.machine  from v$session a,v$process b where a.paddr=b.addr and a.type='USER';

 

SPID       SID USERNAME

------------------------------ ---------- --------------------

PROGRAM        MACHINE

------------------------------ ------------------------------

8303        40 SYS

sqlplus@Database2 (TNS V1-V3)  Database2

 

8321 OS PID        41 TEST SESSION SID

sqlplus@Database1 (TNS V1-V3)  Database1  从Database1上面连接进来的

 

注意生产库这种会话过多会超出连接限制导致后面用户登入不上数据块。要杀这种会话也很简单,我们这边生产库使用下面这条SQL语句查找出session对应的pid,在操作系统层面将oracle进程杀死进而清理session。

 

(3)杀会话

select to_char(a.logon_time, 'yyyy-mm-dd hh24:mi') logon_time,

       a.sql_id,

       a.event,

       a.username,

       a.osuser,

       a.process,

       a.machine,

       a.program,

       a.module,

       b.sql_text,

       b.LAST_LOAD_TIME,

       to_char(b.last_active_time, 'yyyy-mm-dd hh24:mi:ss') last_active_time,

       c.owner,

       c.object_name,

       a.last_call_et,

       a.sid,

       a.SQL_CHILD_NUMBER,

       c.object_type,

       p.PGA_ALLOC_MEM,

       a.p1,

       a.p2,

       a.p3,

       'kill -9 ' || p.spid killstr,

'ps -ef|grep '|| p.spid ||'|grep LOCAL=NO|awk ''{print $2}''|xargs kill -9' kill_sh

  from v$session a, v$sql b, dba_objects c, v$process p

 where a.status ='SNIPED'  --杀死会话状态,还可以是INACTIVE

   and p.addr = a.paddr

   and a.sql_id = b.sql_id(+)

   and a.wait_class='Idle'

   and a.sql_child_number = b.CHILD_NUMBER(+)

   and a.row_wait_obj# = c.object_id(+)

   and a.type = 'USER'

   and a.username='TEST'  --要杀会话的用户的名称

   order by a.sql_id, a.event;

 

红色部分根据具体情况自己修改。

 

 

通过查询出来的语句将会话杀死,可以看到这里PID 8321就是上面test的一个会话对应的PID。

KILL_SH

--------------------------------------------------------------------------------

ps -ef|grep 8321|grep LOCAL=NO|awk '{print $2}'|xargs kill -9

 

[root@Database2 ~]# ps -ef|grep 8321|grep LOCAL=NO|awk '{print $2}'|xargs kill -9

[root@Database2 ~]# ps -ef | grep 8321

root      8397  8270  0 19:28 pts/6    00:00:00 grep 8321