===== Eine Oracle Session beenden - Kill Session / Disconnect Session ==== ==== Einen User in der Oracle Datenbank identifizieren ==== Wird über einen "Dedicated Server Prozess" eine Verbindung mit der Datenbank aufgebaut, lassen sich die die Anwender Prozesse, die mit der Datenbank verbunden sind, über die View V$SESSION identifizieren. Script aufrufen mit dem gesuchten Usernamen und N für das ausblenden der Hintergrund Prozesse (sqlplus>@sessions.sql SYS N): define USER_NAME = &1 define ALL_PROCESS = '&2' prompt prompt Parameter 1 = Username => &&USER_NAME. prompt Parameter 2 = to Show all use Y => &&ALL_PROCESS. prompt set verify off SET linesize 140 pagesize 300 recsep OFF ttitle left "All User Sessions on this DB" skip 2 column inst_id format 99 heading "Inst|ID" column username format a14 heading "DB User|name" column sid format 99999 heading "SID" column serial# format 99999 heading "Serial" column machine format a30 heading "Remote|pc/server" column terminal format a13 heading "Remote|terminal" column program format a16 heading "Remote|program" column module format a16 heading "Remote|module" column client_info format a10 heading "Client|info" column client_identifier format A10 heading "Client|identifier" column OSUSER format a13 heading "OS|User" column LOGON_TIME format a12 heading "Logon|Time" column status format a8 heading "Status" select inst_id , sid , serial# , status , username , machine --, terminal , program , OSUSER , module , to_char(LOGON_TIME,'dd.mm hh24:mi') as LOGON_TIME , client_identifier , client_info from gv$session where ( username like upper('%&&USER_NAME.%') or ( nvl('&ALL_PROCESS.','N')='Y' and username is null)) --and program not like '%(P%)%' order by program ,inst_id / ttitle left "User Sessions Summary on this DB" skip 2 column cs format 9999 column program format A60 column username format A20 COLUMN DUMMY NOPRINT; COMPUTE SUM OF cs ON DUMMY BREAK ON DUMMY; select null dummy , count(*) as cs , username , program from gv$session where username is not null group by username ,program order by username / ttitle off show parameter sessions prompt prompt ... to kill session "ALTER SYSTEM KILL SESSION 'sid,serial#,@inst_id';" prompt ... to end session "ALTER SYSTEM DISCONNECT SESSION 'sid,serial#' IMMEDIATE;" prompt siehe auch [[http://orapowershell.codeplex.com/SourceControl/latest#sql/sessions.sql|sessions.sql]] ==== Eine Sessoin beenden ==== Ist die SID, die SERIAL# und die Instance ID bekannt, kann die Session auf zwei Arten über SQL*Plus beendet werden: * "ALTER SYSTEM KILL SESSION 'sid,serial#,@inst_id';" * "ALTER SYSTEM DISCONNECT SESSION 'sid,serial#' IMMEDIATE;" ==Unterschied zwischen KILL SESSION und DISCONNECT SESSION == Wird eine Session mit "ALTER SYSTEM DISCONNECT SESSION" beendet, wird der dazugehörige User Prozess gleich mit beendet. Bei einem Kill wird die Session nur markiert und abgehängt, der PMON räumt dann später auf. == OS Prozess beenden == Alternativ kann auch der Prozess der Session mit OS Mitteln hart beendet werden: * Windows: orakill ORACLE_SID spid * Linux: kill -9 === Eine gekillete Session in der DB "wiederfinden" ==== Wenn eine Session mit "kill Session" beendet wird wird das Session Objekt (und alle abhängigen Objeckte unter der Session) vom original Eltern Prozesse abgehängt und unter einen "PSEUDO" Prozess gehängt. Der PMON Prozess prüft regelmäßig auf diese "PSEUDO" Prozesse und bereinigt diese. Dies hat aber zur Folge das unter der ursprüngliche PADDR Adresse in der V$SESSION der Prozess nicht mehr gefunden werden kann, eine neue PADDR wird vergeben. Um das nun nach zuverfolgen stehen in der V$SESSION diese Spalten zur Verfügung: * CREATOR_ADDR - state object address of creating process * CREATOR_SERIAL# - serial number of creating process Mit der CREATOR_ADDR kann nun die ADDR Spalte in der V$PROCESS gejoined werden um den gekillten Prozess der alten Session wieder zu finden **ab 11g 11.1.0.6 ** Folgende zwei neue Views helfen die Session besser wieder zu finden: V$PROCESS_GROUP * INDX - Index * NAME - The name of the process group. The default group is called DEFAULT. * PID - Oracle process id V$DETACHED_SESSION * INDX - Index * PG_NAME - The process group name that owns this session. The default group is DEFAULT. * SID - Oracle session id. * SERIAL# - Session serial number. * PID - Oracle process id. ttitle left "Processes without entries in the v$session" skip 2 column process_id format a8 heading "Process|ID" column inst_id format 99 heading "Inst|ID" column username format a8 heading "DB User|name" column osusername format a8 heading "OS User|name" column pname format a8 heading "Process|name" select --p.inst_id to_char(p.spid) as process_id , p.username as osusername , p.pname , p.program from v$process p where p.program!= 'PSEUDO' and p.addr not in (select gv.paddr from v$session gv) and p.addr not in (select bg.paddr from v$bgprocess bg) and p.addr not in (select ss.paddr from v$shared_server ss) --order by p.inst_id / -- new column creator_addr in v$session! ttitle left "get the prozess of a killed session with the help of the creator_addr" skip 2 select --p.inst_id to_char(p.spid) as process_id , p.username as osusername , p.pname , p.program from v$process p where p.addr in (select gv.creator_addr from v$session gv where status in ('KILLED') ) / ttitle off === SNIPED Sessions ==== .. Sniped - the session has passed the idle_time limit defined in user profile. The session will remain snipped until the client communicates with the db again, when it will get "ORA-02396: exceeded maximum idle time, please connect again" and the session is removed from v$session. ... When IDLE_TIME is set in the users' profiles or the default profile. This will kill the sessions in the database (status in v$session now becomes SNIPED) and they will eventually disconnect. It does not always clean up the Unix session (LOCAL=NO sessions). At this time all oracle resources are released but the shadow processes remains and OS resources are not released. This shadow process is still counted towards the parameters of init.ora. This process is killed and entry from v$session is released only when user again tries to do something. Another way of forcing disconnect (if your users come in via SQL*Net) is to put the file sqlnet.ora on every client machine and include the parameter "SQLNET.EXPIRE_TIME" in it to force the close of the SQL*Net session. ... see https://community.oracle.com/thread/531765 In Arbeit ==== Quellen ==== Netz: * http://oracle-base.com/articles/misc/killing-oracle-sessions.php#disconnect_session Oracle Support: * How To Find The Process Identifier (pid, spid) After The Corresponding Session Is Killed? (Doc ID 387077.1)