Home >Database >Mysql Tutorial >ORACLE定期清理INACTIVE会话

ORACLE定期清理INACTIVE会话

WBOY
WBOYOriginal
2016-06-07 15:03:161471browse

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入 另外,由于kill session是直接将session kill掉,有可能出现导致事物回滚的现象,其实我们可以使用disconnect session完成当前事务并终止session.这种方式比alter system kill session跟安全可靠。

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入

 

  另外,由于kill session是直接将session kill掉,有可能出现导致事物回滚的现象,其实我们可以使用disconnect session完成当前事务并终止session.这种方式比alter system kill session跟安全可靠。

  1: CREATE OR REPLACE PROCEDURE SYS.DB_KILL_IDLE_CLIENTS AUTHID DEFINER AS

  2:    job_no number;

  3:    num_of_kills number := 0;

  4: BEGIN

  5:

  6:    FOR REC IN

  7:        (SELECT SID, SERIAL#, INST_ID, MODULE,STATUS

  8:         FROM gv$session S

  9:             WHERE S.USERNAME IS NOT NULL

  10:             AND UPPER(S.PROGRAM) IN ('xxxx', 'xxxx')

  11:                  AND S.LAST_CALL_ET >= 2*60*60

  12:             AND S.STATUS'KILLED'

  13:         ORDER BY INST_ID ASC

  14:             ) LOOP

  15:          ---------------------------------------------------------------------------

  16:          -- kill inactive sessions immediately

  17:          ---------------------------------------------------------------------------

  18:          DBMS_OUTPUT.PUT('LOCAL SID ' || rec.sid || '(' || rec.module || ')');

  19:          execute immediate 'alter system disconnect session ''' || rec.sid || ', ' ||

  20:                             rec.serial# || '''immediate' ;

  21:

  22:          DBMS_OUTPUT.PUT_LINE('. killed locally ' || job_no);

  23:          num_of_kills := num_of_kills + 1;

  24:    END LOOP;

  25:    DBMS_OUTPUT.PUT_LINE ('Number of killed system sessions: ' || num_of_kills);

  26: END DB_KILL_IDLE_CLIENTS;

  27: /

  然后,我们可以在作业(JOB)或Schedule里面定期调用该存储过程,也可以通过后台作业结合shell脚本实现定期清理空闲会话的功能。例如如下所示。

  创建killSession.sh脚本,调用该存储过程SYS.DB_KILL_IDLE_CLIENTS

  1: #!/bin/bash

  2:

  3:

  4:

  5: logfile=/home/oracle/cron/session/log/killSession.log

  6:

  7: echo " " 》 $logfile 2>&1

  8: echo "START ----`date`" 》 $logfile 2>&1

  9: sqlplus /nolog 《STATS

  10: connect / as sysdba

  11: exec sys.db_kill_idle_clients;

  12: exit;

  13: STATS

  14:

  15: echo "END ------`date`" 》 $logfile 2>&1

  在crontab里面配置后台作业,每隔15分钟运行一次,清理哪些满足条件的空闲会话。

  0,15,30,45 * * * * /home/oracle/cron/session/bin/killSession.sh >/dev/null 2>&1

  [1] [2] 

ORACLE定期清理INACTIVE会话

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn