71

Is there a better way to forcefully disconnect all users from an Oracle 10g database schema than restarting the Oracle database services?

We have several developers using SQL Developer connecting to the same schema on a single Oracle 10g server. The problem is that when we want to drop the schema to rebuild it, inevitably someone is still connected and we cannot drop the database schema or the user while someone is still connected.

By the same token, we do not want to drop all connections to other schemas because other people may still be connected and testing with those schemas.

Anyone know of a quick way to resolve this?

7 Answers 7

121

To find the sessions, as a DBA use

select sid,serial# from v$session where username = '<your_schema>'

If you want to be sure only to get the sessions that use SQL Developer, you can add and program = 'SQL Developer'. If you only want to kill sessions belonging to a specific developer, you can add a restriction on os_user

Then kill them with

alter system kill session '<sid>,<serial#>'

(e.g. alter system kill session '39,1232')

A query that produces ready-built kill-statements could be

select 'alter system kill session ''' || sid || ',' || serial# || ''';' from v$session where username = '<your_schema>'

This will return one kill statement per session for that user - something like:

alter system kill session '375,64855';

alter system kill session '346,53146';

4
  • I found that in our RAC environment, killing the session often left it hanging, but finding out the process id and killing it from the operating system always worked.
    – Jonathan
    Sep 25, 2008 at 18:45
  • 4
    small addition - add immediate to the command : select 'alter system kill session ''' || sid || ',' || serial# || ''' immediate;' from v$session where username = '<your_schema>'; this will force the command
    – Michael
    May 14, 2012 at 11:23
  • nice answer. Exactly what i was looking for. Dec 17, 2014 at 15:48
  • And probably obvious but username ('<your_schema>' above) should be in uppercase
    – rshdev
    Apr 3, 2022 at 19:32
19

Find existing sessions to DB using this query:

SELECT s.inst_id,
       s.sid,
       s.serial#,
       p.spid,
       s.username,
       s.program
FROM   gv$session s
       JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id
WHERE  s.type != 'BACKGROUND';

you'll see something like below. Oracle Sessions

Then, run below query with values extracted from above results.

ALTER SYSTEM KILL SESSION '<put above s.sid here>,<put above s.serial# here>';

Ex: ALTER SYSTEM KILL SESSION '93,943';

9

my proposal is this simple anonymous block:

DECLARE
   lc_username   VARCHAR2 (32) := 'user-name-to-kill-here';
BEGIN
   FOR ln_cur IN (SELECT sid, serial# FROM v$session WHERE username = lc_username)
   LOOP
      EXECUTE IMMEDIATE ('ALTER SYSTEM KILL SESSION ''' || ln_cur.sid || ',' || ln_cur.serial# || ''' IMMEDIATE');
   END LOOP;
END;
/
1
  • 2
    better to note ospid before killing sessions, they may got stuck Oct 10, 2014 at 10:51
1

Make sure that you alter the system and enable restricted session before you kill them or they will quickly log back into the database before you get your work completed.

1
  • 2
    Might be easier, and less intrusive, to lock the user he intends to drop, then kill off all connected sessions on that user, then drop it and re-create. No need to unnecessarily restrict access to other users. Feb 23, 2012 at 23:02
0

Just my two cents : the best way (but probably not the quickest in the short term) would probably be for each developer to work on his own database instance (see rule #1 for database work).

Installing Oracle on a developer station has become a no brainer since Oracle Database 10g Express Edition.

3
  • Only for Windows and certain flavors of Linux. Oct 2, 2012 at 20:27
  • That's not really relevant to the question?
    – Konrads
    Dec 9, 2016 at 14:58
  • It might not be the answer the asker was looking for and I can perfectly understand why it has not been upvoted very much, but I think it is very relevant considering the use case he specifically put forward. Multiple developers working on the same database is a very bad idea, and this problem is definitely not the worst that will happen.
    – Mac
    Dec 9, 2016 at 18:10
0

Have you tried ALTER SYSTEM KILL SESSION? Get the SID and SERIAL# from V$SESSION for each session in the given schema, then do

ALTER SCHEMA KILL SESSION sid,serial#;

0
0

just use SQL :

disconnect; 

conn tiger/scott as sysdba;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.