Keywords: Oracle Database | User Management | Session Termination | v$session View | ALTER SYSTEM Command
Abstract: This article provides an in-depth analysis of the ORA-01940 error encountered when dropping users in Oracle databases and presents complete technical solutions. By examining naming conventions in v$session view, session termination mechanisms, and system-level operations, it offers a comprehensive workflow from session querying to forced deletion. The paper details proper methods for querying active sessions, using ALTER SYSTEM KILL SESSION commands, and compares different approaches' applicability and risks, serving as a practical guide for database administrators.
Problem Background and Error Analysis
In Oracle database administration practice, dropping user accounts is a common maintenance operation. However, when the target user currently has active connections, executing the DROP USER command triggers ORA-01940 error: "cannot drop a user that is currently connected." This protection mechanism prevents data inconsistency and operational disruption but also creates operational challenges for administrators.
Core Solution: Query and Terminate Active Sessions
To successfully drop a connected user, it is essential to first identify and terminate all active sessions. Oracle's data dictionary view v$session stores all current session information, but a crucial detail must be noted: usernames are stored in uppercase in this view. This is a key point often overlooked by administrators.
The following query demonstrates how to correctly retrieve active session information for target users:
SELECT s.sid, s.serial#, s.status, p.spid
FROM v$session s, v$process p
WHERE s.username = 'TEST'
AND p.addr(+) = s.paddr
Key improvements in this code include:
- Explicitly specifying username in uppercase 'TEST' to ensure accurate matching
- Using outer join to associate
v$processview, obtaining operating system process IDs - Returning critical information including session ID, serial number, status, and process ID
Session Termination Operation
After obtaining valid SID and SERIAL# values, specific sessions can be terminated using:
ALTER SYSTEM KILL SESSION '<SID>, <SERIAL>'
In practical operation, replace <SID> and <SERIAL> with actual values from the query. For example, if query results show SID=123 and SERIAL#=456, the command should be:
ALTER SYSTEM KILL SESSION '123, 456'
Alternative Approach Analysis
Beyond the standard method, a more aggressive solution exists: dropping users through database restart. This approach involves the following steps:
-- Log in as sysdba
sqlplus / as sysdba
-- Shutdown database immediately
SHUTDOWN IMMEDIATE;
-- Start in restricted mode
STARTUP RESTRICT;
-- Drop user
DROP USER TEST CASCADE;
-- Return to normal operation (optional)
SHUTDOWN IMMEDIATE;
STARTUP;
While effective, this method has significant drawbacks:
- Requires complete database restart, affecting all users and services
- In restricted mode, only users with RESTRICTED SESSION privilege can connect
- Not suitable for production environments requiring high availability
Best Practice Recommendations
Based on comparative analysis of both methods, we recommend the following operational workflow:
- Session Query Phase: Always query
v$sessionview using uppercase usernames to ensure query accuracy - Session Termination Phase: Prioritize
ALTER SYSTEM KILL SESSIONcommand to target specific sessions - User Drop Phase: Execute
DROP USER ... CASCADEcommand after confirming all sessions are terminated - Exception Handling: Consider database restart approach only if standard methods fail, assessing business impact
Technical Key Points Summary
The technical solutions discussed involve multiple core Oracle database concepts:
- Data Dictionary Views:
v$sessionandv$processviews provide real-time session and process information - Session Management: Oracle uniquely identifies each session through SID and SERIAL#, forming the foundation of session control
- Privilege Control:
ALTER SYSTEMcommand requires appropriate system privileges, typically executed by DBAs - Transaction Integrity: Forceful session termination may cause uncommitted transactions to roll back, requiring business-level impact assessment
By understanding these underlying mechanisms, database administrators can perform user management operations more safely and efficiently, ensuring stable operation of database systems.