Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill all active and inactive oracle sessions for user

I am trying the below script to kill all active and inactive oracle sessions for user at once but it doesn't work. The script executes successfully but does not kill sessions for user.

BEGIN   FOR r IN (select sid,serial# from v$session where username = 'USER')   LOOP     EXECUTE IMMEDIATE 'alter system kill session ''' || r.sid        || ',' || r.serial# || '''';   END LOOP; END; 
like image 200
Andrew Avatar asked Jul 03 '15 08:07

Andrew


People also ask

Can we kill INACTIVE sessions in Oracle?

You can terminate sessions with the ALTER SYSTEM KILL command. When you issue the ALTER SYSTEM KILL session command, you must specify the session's index number and serial number. To identify the session index number (sid) and serial number of a session, query the V$SESSION dynamic performance view as shown below.

How do I automatically kill an INACTIVE session in Oracle?

alter system kill session 'SID,SERIAL#' immediate; it works fine and load become normal. So how to set the time then my inactive session will kill automatically so load become not high pls suggest. dba_profiles all limit is unlimited.

What is active and INACTIVE sessions in Oracle?

ACTIVE means the session is currently executing some SQL operations whereas INACTIVE means the opposite. Check out the ORACLE v$session documentation.


1 Answers

The KILL SESSION command doesn't actually kill the session. It merely asks the session to kill itself. In some situations, like waiting for a reply from a remote database or rolling back transactions, the session will not kill itself immediately and will wait for the current operation to complete. In these cases the session will have a status of "marked for kill". It will then be killed as soon as possible.

Check the status to confirm:

SELECT sid, serial#, status, username FROM v$session; 

You could also use IMMEDIATE clause:

ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE; 

The IMMEDIATE clause does not affect the work performed by the command, but it returns control back to the current session immediately, rather than waiting for confirmation of the kill. Have a look at Killing Oracle Sessions.

Update If you want to kill all the sessions, you could just prepare a small script.

SELECT 'ALTER SYSTEM KILL SESSION '''||sid||','||serial#||''' IMMEDIATE;' FROM v$session; 

Spool the above to a .sql file and execute it, or, copy paste the output and run it.

like image 67
Lalit Kumar B Avatar answered Sep 20 '22 06:09

Lalit Kumar B