I am trying to drop a user from an Oracle DB (version 12c), but can not get it to work. The error I am getting is:
ORA-01940: cannot drop a user that is currently connected
Naturally I looked around, and found out how to forcibly disconnect and kill a session. So I used the query:
select s.sid, s.serial#, status, s.username
from v$session s
where username = 'user_i_want_to_drop';
and then killed the only active session by using
alter system kill session '<sid>,<serial#>' IMMEDIATE;
naturally using the values from the query.
When I run the query again, it comes up empty, as expected. However, I still cannot drop the user and get the same error message.
I have noticed that when I query on gv$session, two sessions for that user show up. However, I cannot kill those using the alter system kill session statement I used above.
What am I missing here?
The other session(s) are connected to another instance of your cluster.
Add inst_id to your gv$session query, like this:
select sid,serial#,inst_id
from gv$session
where username = 'user_i_want_to_drop';
Then, include the inst_id in the alter system kill session command, like this:
alter system kill session '<sid>,<serial#>,#<inst_id>' immediate;
E.g.,
alter system kill session '15,1891,#1' immediate;
Related
After killing session by using ALTER SYSTEM KILL SESSION, I tried to drop the user, but it says the user is connected.
So when I again killed the session and tried to drop the user, I observed that each time I kill a session, immediately another session is getting created.
Example :
I run the query "Drop user cascade" : it says user connected cannot drop
I run the query:
SELECT 'ALTER SYSTEM KILL SESSION '''||s.sid||','|| s.serial#||''';'
FROM v$session s, v$process p
WHERE lower(s.username) = 'username'
AND p.addr(+) = s.paddr and status='INACTIVE';
I get a query to kill sessions, now I kill all the sessions and run the drop user command again, it says user connected.
Checking for sessions again on the user shows a single inactive session which is getting created each time.
Dropping a user for an application that quickly reconnects may require a few steps:
Prevent the user from reconnecting with alter user username account lock
Kill the user's sessions with alter system kill session ...
Wait for any long-running transactions to rollback. Monitor the rollback progress by repeatedly running select used_urec, gv$transaction.* from gv$transaction
Drop the user with drop user username
Is there a way for a user to terminate one's own session/connections, given an Oracle SID, without DBA rights?
Specifically, I can run this in my DB without admin rights:
SELECT SID, "SERIAL#", STATUS, USERNAME
FROM V$SESSION
WHERE
(USERNAME = 'LastF')
AND
(STATUS = 'INACTIVE');
But when I go to kill my orphaned session (from another session to which I still have access),
ALTER SYSTEM KILL SESSION "12, 123"
I get the following:
JDBC ERROR: ORA-01031: insufficient privileges
Note: I am connecting with JDBC through R/Rstudio using the RJDBC package.
Motivation:
It doesn't appear too difficult to kill sessions in Oracle SQL:
https://docs.oracle.com/cd/B28359_01/server.111/b28310/manproc008.htm#ADMIN11192
How can I kill all sessions connecting to my oracle database?
However, for non-DBA users who have orphaned connections (i.e. internet outage, 3rd party client that manages connections errors out, etc), it can be really frustrating to get:
ORA-02391 exceeded simultaneous SESSIONS_PER_USER limit
and have to wait for timeout.
To successfully run an ALTER SYSTEM command, you don't need to be the DBA, but you do need the ALTER SYSTEM privilege to be granted to you (or to the "user" owning the application through which you connect to the database - which may be different from "you" as the "user" of RStudio).
You have a few options:
ask the DBA to kill the session
ask to be granted the ALTER SYSTEM privilege (which is a very poor practice)
have a "supervisor" (however defined - responsible specifically for these situations) be granted the ALTER SYSTEM privilege, who will be in charge of killing such sessions
(perhaps the best option) create a packaged
procedure whose only task is to kill orphaned sessions. Grant ALTER SYSTEM to the package owner, and grant execute privilege on that
package to individual users (as needed). The procedure should be
written to only kill specific kinds of sessions.
You can use the below to cancel whatever is running in the session
DECLARE
l_status v$session.status%TYPE;
BEGIN
dbms_system.set_ev( &sid, &serial, 10237, 1, '');
LOOP
SELECT status INTO l_status FROM v$session
WHERE sid = &sid and serial# = &serial;
EXIT WHEN l_status='INACTIVE';
END LOOP;
dbms_system.set_ev( &sid, &serial, 10237, 0, '');
END;
you will have to create a direct select grant on sys.v_$session
grant select on v_$session to
Where is the schema that owns the above procedure. This has to be a direct grant and not through a role.
Check the link for more details and given by Donald Burleson
we can kill the oracle session with pid ,
if you are unable to identify the operating system process identifier (spid) from the query , you can issue the following query to help identify the correct session:
SELECT s.sid, s.serial#, p.spid
FROM v$process p, v$session s
WHERE p.addr = s.paddr
AND s.username = '<username>';
At the operating system prompt, issue the kill command and supply the operating system process identifier (spid):
kill <spid>
How I can make a trigger that a user connect to database check if user System is connected in this moment. Is correct? Then error message and user no connect to database.
Thanks guys.
First, I want to point out that it sounds like you're trying to recreate the functionality of the command ALTER SYSTEM ENABLE RESTRICTED SESSION. You might consider just using that instead.
On the topic of your question, there's an easy answer, but it isn't very good.
CREATE OR REPLACE TRIGGER logon_system_maintenance
AFTER LOGON on DATABASE
IS
system_is_connected varchar2(1) := 'N';
BEGIN
select 'Y' into system_is_connected
from v$session
where username = 'SYSTEM' and status = 'ACTIVE';
-- this will not prevent users with ADMINISTER DATABASE TRIGGER privilege from connecting
RAISE_APPLICATION_ERROR (-20001, 'SYSTEM user is performing maintenance, please try again later');
EXCEPTION
WHEN NO_DATA_FOUND THEN
null; -- system not connected, OK
WHEN OTHERS THEN
null; -- probably the user doesn't have permission to view V$SESSION!
-- should they be able to connect, or not?
END;
/
The problem here is that in order to check if SYSTEM is connected, the user connecting has to be able to view the V$SESSION view, which means they need the SELECT_CATALOG_ROLE role. Probably most of your users don't have this role, which means they don't have permission to even check if SYSTEM is connected or not!
In my experience, what most applications do is create a table to hold various system parameters (e.g. MY_PARAM_TABLE), and add a parameter which controls whether users can log in or not (e.g. SYSTEM_MAINTENANCE = 'N'). Then when you log on as SYSTEM, you set that flag to Y, and the trigger checks that table (which all users should be able to access) and denies access until you set it back to N.
Also keep in mind that you can't prevent DBA users from logging in this way.
The original owner of the Database has left the company, so I want to change the owner to myself, however, it failed.
When I tried through SSMS: Database properties->Files->Owner, it give the error message like:
Set owner failed for Database XYZ.
An exception occurred while executing a Transact-SQL statement or batch.
Lock request time out period exceeded. (Microsoft SQL Server, Error: 1222)
And when I tried through script with the query:
ALTER AUTHORIZATION ON DATABASE::XYZ TO [MyUserName]
The query seems blocked and run forever without success.
Can anybody give some help?
Queries using dbo-owned objects will acquire a schema stability lock on the existing database owner principal. The ALTER AUTHORIZATION will need a schema modification lock on the same principal, and are thus blocked due to the incompatible lock. You can query sys.dm_tran_locks to identify the blocking sessions.
I have a database 'My Database' which I would like to rename so that there is no white space. I tried to rename it using
use master
exec sp_renamedb 'I 3 SCI Study','I3SciStudy'
and was greeted with the error
Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation.
This server is my local machine and I have no other query windows open but the window in which I ran the rename query. Is there some sort of close connection command that I need to run before I can rename the database?
Try this command, but caution is advised:
USE master;
ALTER DATABASE [dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE [dbname] SET MULTI_USER;
GO
Also you can interrogate information about currently active lock manager resources.
SELECT *
FROM sys.dm_tran_locks DTL
WHERE DTL.[resource_database_id] = DB_ID()
Each row represents a currently active request to the lock manager for a lock that has been granted or is waiting to be granted. You will see not only your request on current database(most likely with resoure_type DATABASE). It is impossible to change a database name while these resources are locked
Use SSMS to rename the database, you shouldn't have any problem doing it that way.