In Oracle, to kill sniped sessions dynamically using SQL, you can use EXECUTE IMMEDIATE
inside a PL/SQL block.
Understanding Sniped Sessions
A sniped session is a session that has been marked as inactive (due to profile settings like IDLE_TIME
in a resource profile), but it is still present in v$session
and v$process
. Such sessions must be explicitly killed using ALTER SYSTEM KILL SESSION
or SYS.DBMS_SYSTEM.KILL_SESSION
.
Procedure to Kill Sniped Sessions Dynamically
This procedure:
- Finds sessions in
v$session
with status = 'SNIPED'. - Uses
EXECUTE IMMEDIATE
to kill them.
PL/SQL Code
Alternative: Killing Using SYS.DBMS_SYSTEM.KILL_SESSION
Instead of ALTER SYSTEM KILL SESSION
, you can use DBMS_SYSTEM.KILL_SESSION
:
Automating with a Scheduled Job
You can automate sniped session killing using DBMS_SCHEDULER:
or
Alternatively you can automate sniped session killing using DBMS_JOB also:
declare
jobno number;
begin
dbms_job.submit(jobno, 'kill_sniped_sessions;',sysdate,'TRUNC(SYSDATE,''mi'')+5/1440'); -- Run every 5 minutes
end;
/
Key Takeaways
- Identify sniped sessions from
v$session
wherestatus = 'SNIPED'
. - Use
EXECUTE IMMEDIATE
to dynamically kill sessions. - Ensure you have
ALTER SYSTEM
privileges to kill sessions. - Consider automating this process using
DBMS_SCHEDULER
. - Grant select on v_$session must be given to the user if run other than SYS user
Tags:
Oracle