How to List Active and Open Connections in Oracle
When managing an Oracle database, there are times when you need to issue a SHUTDOWN IMMEDIATE
command. Before doing so, it’s essential to identify all active sessions to avoid disrupting critical operations. Here’s how you can list active and open connections in your Oracle database.
Query to List Active User Sessions
The following SQL query retrieves information about all active user sessions in the database:
SELECT
SUBSTR(a.spid, 1, 9) AS pid,
SUBSTR(b.sid, 1, 5) AS sid,
SUBSTR(b.serial#, 1, 5) AS ser#,
SUBSTR(b.machine, 1, 6) AS box,
SUBSTR(b.username, 1, 10) AS username,
b.server,
SUBSTR(b.osuser, 1, 8) AS os_user,
SUBSTR(b.program, 1, 30) AS program
FROM v$session b, v$process a
WHERE b.paddr = a.addr
AND b.type = 'USER'
ORDER BY spid;
Explanation of the Query
- Tables Used:
v$session
: Contains session details for all current connections.v$process
: Links sessions to operating system processes.
- Columns Selected:
spid
: Operating system process ID of the session.sid
: Oracle session identifier.serial#
: Serial number to uniquely identify sessions.machine
: Host machine name.username
: Oracle username for the session.server
: Connection type (DEDICATED
orSHARED
).osuser
: Operating system user of the session.program
: Application program initiating the session.
- Filters Applied:
b.type = 'USER'
: Filters to include only user sessions (ignores background processes).
- Ordering:
- Sorts results by
spid
for clarity.
- Sorts results by
Sample Output
PID | SID | SER# | BOX | USERNAME | SERVER | OS_USER | PROGRAM |
---|---|---|---|---|---|---|---|
12345 | 101 | 12 | HR01 | HR_ADMIN | DEDICATED | hradmin | sqlplus.exe |
12346 | 102 | 34 | FIN01 | FIN_MANAGER | SHARED | finmgr | Oracle Financials App |
Use Cases
- Before shutting down the database, identify active users and notify them before issuing a shutdown.
- Monitoring User Activity: Track which users or programs are connected to your database.
- Performance Troubleshooting: Identify long-running or resource-heavy sessions.
Tips for Managing Sessions
- Kill inactive or unnecessary sessions:
If a session is idle but consuming resources, you can terminate it:ALTER SYSTEM KILL SESSION 'SID,SERIAL#';
- View System Sessions: To include all sessions (user and system processes), remove the
b.type = 'USER'
filter. - Monitor Continuously: Use tools like Oracle Enterprise Manager or custom scripts to automate session monitoring.
Conclusion
By running the above SQL query, you can effectively list all active and open connections in your Oracle database. This ensures you can monitor user activity and prevent disruptions during database maintenance.
Would you like additional tips on managing user sessions or monitoring database activity? 😊