Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping a connected user from an Oracle 10g database schema

Tags:

oracle

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?

like image 400
Joshua Starner Avatar asked Sep 17 '08 17:09

Joshua Starner


People also ask

How do I drop a user from an Oracle Database?

Dropping user in Oracle remove user and it’s contents from the database. You must use CASCADE keyword to remove all objects owned by the schema. Sometimes users are connected to the database and it takes long time to drop. So in this case you can drop forcefully by killing user session connected to the database.

Why can’t I drop a database schema while someone is connected?

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.

How do I drop a user whose schema contains objects?

You must specify this clause to drop a user whose schema contains any objects. If the user's schema contains tables, then Oracle Database drops the tables and automatically drops any referential integrity constraints on tables in other schemas that refer to primary and unique keys on these tables.

How to remove a user from an oracle schema?

If the user whose schemas contain objects such as views and tables, you need to delete all schema objects of the user first and then drop the user. Deleting all schema objects of the users first before removing the user is quite tedious. Therefore, Oracle provides you with the CASCADE option.


2 Answers

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';

like image 171
Sten Vesterli Avatar answered Sep 21 '22 09:09

Sten Vesterli


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';

like image 28
Chand Priyankara Avatar answered Sep 22 '22 09:09

Chand Priyankara