在一些写法不够严谨的程序代码设计上,一些数据库连接,在处理完数据请求后,没有主动释放或关闭,从而导致数据库层面积累了大量的无用连接,这些连接占用大量的数据库资源,在无法对代码做调整的情况下,可以从数据库层面主动结束或关闭这些会话连接,下面是针对这个问题设计的存储过程:

CREATE OR REPLACE PROCEDURE DB_KILL_FRNT_CLIENTS  AS
  job_no       number;
  num_of_kills number := 0;
BEGIN

  FOR REC IN (SELECT SID, SERIAL#, MODULE, STATUS
                FROM v$session S
               WHERE username = 'FRNT'
                 and status = 'SNIPED') LOOP
    ---------------------------------------------------------------------------
    -- kill FRNT SNIPED sessions immediately
    ---------------------------------------------------------------------------
    DBMS_OUTPUT.PUT_LINE('LOCAL SID ' || rec.sid || '(' || rec.module || ')');
    execute immediate 'alter system kill session ''' || rec.sid || ', ' ||
                      rec.serial# || '''immediate';

   DBMS_OUTPUT.PUT_LINE('. killed locally ' || job_no);
    num_of_kills := num_of_kills + 1;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('Number of killed xxxx system sessions: ' ||
                       num_of_kills);
END DB_KILL_FRNT_CLIENTS;

然后封装成Bash sh脚本调用上述存储过程:

#/bin/sh
source ~/.bash_profile
/oracle/app/db/11.2.0/bin/sqlplus "/as sysdba" << EOF
spool /home/oracle/kill_frnt_session.log
exec DB_KILL_FRNT_CLIENTS
spool off
exit
EOF

直接在LINUX下定时任务调用:

30 * * * * /home/oracle/kill_frnt_session.sh