How to kill all active and inactive oracle sessions for user

后端 未结 6 904
北海茫月
北海茫月 2021-01-30 14:30

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 f

相关标签:
6条回答
  • 2021-01-30 14:50

    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.

    0 讨论(0)
  • 2021-01-30 14:53

    Execute this script:

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

    It will printout sqls, which should be executed.

    0 讨论(0)
  • 2021-01-30 14:54
    BEGIN
      FOR r IN (select sid,serial# from v$session where username='user')
      LOOP
          EXECUTE IMMEDIATE 'alter system kill session ''' || r.sid  || ',' 
            || r.serial# || ''' immediate';
      END LOOP;
    END;
    

    This should work - I just changed your script to add the immediate keyword. As the previous answers pointed out, the kill session only marks the sessions for killing; it does not do so immediately but later when convenient.

    From your question, it seemed you are expecting to see the result immediately. So immediate keyword is used to force this.

    0 讨论(0)
  • 2021-01-30 15:00

    inactive session the day before kill

    begin
        for i in (select * from v$session where status='INACTIVE' and (sysdate-PREV_EXEC_START)>1)
        LOOP
            EXECUTE IMMEDIATE(q'{ALTER SYSTEM KILL SESSION '}'||i.sid||q'[,]'  ||i.serial#||q'[']'||' IMMEDIATE');
        END LOOP;
    end;

    0 讨论(0)
  • 2021-01-30 15:06
    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;
    /
    

    It works for me.

    0 讨论(0)
  • 2021-01-30 15:08

    For Oracle 11g this may not work as you may receive an error like below

    Error report:
    SQL Error: ORA-00026: missing or invalid session ID
    00026. 00000 -  "missing or invalid session ID"
    *Cause:    Missing or invalid session ID string for ALTER SYSTEM KILL SESSION.
    *Action:   Retry with a valid session ID.
    

    To rectify this, use below code to identify the sessions

    SQL> select inst_id,sid,serial# from gv$session or v$session

    NOTE : v$session do not have inst_id field

    and Kill them using

    alter system kill session 'sid,serial,@inst_id' IMMEDIATE;
    
    0 讨论(0)
提交回复
热议问题