Clearing Down Old Database Sessions
This article explains how you might manage large numbers of old or inactive database sessions.
oracle miscconfigurationintermediate
by OracleDba
38 views
This article explains how you might manage large numbers of old or inactive database sessions.
123456789101112131415161718
-- Kill sessions older than 2 hours.
CREATE PROFILE old_session_profile LIMIT
CONNECT_TIME 120
/
-- Kill sessions that have been inactive for 1 hour.
CREATE PROFILE inactive_session_profile LIMIT
IDLE_TIME 60
/
-- Kill sessions older than 2 hours or inactive for 1 hour.
CREATE PROFILE old_or_inactive_sess_profile LIMIT
CONNECT_TIME 120
IDLE_TIME 60
/
-- Assign relevant profile to a user.
ALTER USER my_user PROFILE old_or_inactive_sess_profile;1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
CONN sys@pdb1 as sysdba
CREATE USER admin_tasks_user IDENTIFIED BY MyPassword1;
GRANT ALTER SYSTEM TO admin_tasks_user;
GRANT SELECT ON v_$session TO admin_tasks_user;
ALTER USER admin_tasks_user ACCOUNT LOCK;
CONN sys@pdb1 as sysdba
CREATE OR REPLACE PROCEDURE admin_tasks_user.kill_old_report_sessions AS
BEGIN
FOR cur_rec IN (select 'alter system kill session ''' || sid || ',' || serial# || '''' AS ddl
from v$session
where LOWER(module) = 'financial reports'
and username = 'REPORTS_USER'
and logon_time < SYSDATE-(1/24)
)
LOOP
BEGIN
EXECUTE IMMEDIATE cur_rec.ddl;
EXCEPTION
WHEN OTHERS THEN
-- You probably need to log this error properly here.
-- I will just re-raise it.
RAISE;
END;
END LOOP;
END;
/
GRANT EXECUTE ON admin_tasks_user.kill_old_report_sessions TO test;
CONN test/test@pdb1
EXEC admin_tasks_user.kill_old_report_sessions;
BEGIN
DBMS_SCHEDULER.create_job (
job_name => 'test.kill_old_report_sessions_job',
comments => 'Kill old reports if they have been running for longer than 1 hour.',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN admin_tasks_user.kill_old_report_sessions; END;',
start_date => SYSTIMESTAMP,
repeat_interval => 'freq=hourly; byminute=0,15,30,45; bysecond=0;',
enabled => TRUE);
END;
/Please to add comments
No comments yet. Be the first to comment!