Killing An Oracle Session Using SQL

Sometimes the need arises for me to terminate a connection or two to Oracle from SQL. Applications with connections to Oracles does not always terminate their connections properly, and Oracle some times does not discover them as dead. Luckily, you can discover them through SQL into Oracles data-dictionary, and kill them using a quick alter system command. Here is how.

Login Using SQLPlus

You start by logging into the instance, that you want to kill connections to. In this example, I login as sysdba. You will need to login with someone with proper privileges (alter system).

sqlplus /nolog
connect / as sysdba

Find Sessions To Kill

Then, you will need to query the v$session table, to obtain a SID-id and serial#-number combination, which you will need to kill the session.

select sid,serial#,username from v$session

Which gives something like this:

       SID    SERIAL# USERNAME
---------- ---------- ------------------------------
       126      61720 foo
       127      51013 bar
       135      18809 bleech
       136      48908 bleech

Say we want to terminate the bar user session, you will need both the SID value 127 and the serial# value 51013.

Kill The Sessions

Then fire off an alter session SQL like this:

alter system kill session '127,51013';

If this does not kill it, you can try adding immediate to the alter, like this:

alter system kill session '127,51013' immediate;

你可能感兴趣的:(Killing An Oracle Session Using SQL)