Do you have trouble finding timed-out sessions and killing them? This script will help you find out the Unix process ID (PID) for the timed-out sessions. You can use this PID to kill the session from the operating system.
In the case of an app server malfunction or crash, sessions may not get closed properly. You may not be able to kill the session using the "alter system kill session" command. The best thing you can do is find out the UNIX process and kill it, which will in turn close the unwanted or timed-out session. This will also help to keep the total number of UNIX process under control.
I have found this script useful in OLTP databases, especially when you have connections from application servers or java clients. The total number of processess that can be spawned by the operating system may be excessive if the timed-out sessions' processes are active. (The total number of process in Solaris, for example, is decided by the parameter in the "/etc/system" file).
This script can be used to monitor the session status in detail and to control any user sessions that have the habit of not closing the connection properly. This query (which uses v$session and v$process) has been tested on Oracle 8.1.7 on Solaris 7 and 8.
select s.username, s.sid, s.serial#, p.pid ppid, s.status, s.machine, s.osuser, substr(s.program,1,20) USER_PROGRAME, s.process USER_PROCESS, substr(p.program,1,20) SERVER_PROGRAME,
to_char(p.spid) spid, to_char(logon_time, 'mm/dd/yy hh24:mi:ss') logon_time, -- idle time -- DAYS --( trunc(LAST_CALL_ET/86400) * 24 ) || ':' || -- days separately substr('0'||trunc(LAST_CALL_ET/86400),-2,2) || ':' || -- hours substr('0'||trunc(mod(LAST_CALL_ET,86400)/3600),-2,2) || ':' || -- minutes substr('0'||trunc(mod(mod(LAST_CALL_ET,86400),3600)/60),-2,2) || ':' || --seconds substr('0'||mod(mod(mod(LAST_CALL_ET,86400),3600),60),-2,2) idle_time from v$session s, v$process p where s.username is not null and p.addr(+) = s.paddr -- UNCOMMENT THE NEXT LINE TO SEE YOUR OWN SESSION ONLY --and userenv('SESSIONID') = s.audsid order by username, sid;
George T. writes: First, this is a very good script. However I was expecting (from the text of the tip) that it would only show the sessions that have timed out. It shows all user (not including Oracle's processes) sessions on my 8i database. The tip says "This script will help you find out the Unix process ID (PID) for the timed out sessions," which I understood to be only those sessions that have timed out. Is this correct? If so, maybe it is important to note that ACTIVE users will show up in the results of this script. You cannot pipe the results of this script to the 'kill'.
For More Information
- Feedback: E-mail the editor with your thoughts about this tip.
- More tips: Hundreds of free Oracle tips and scripts.
- Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
- Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
- Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
- Best Web Links: Oracle tips, tutorials, and scripts from around the Web.
This was first published in November 2002