Killing Oracle Sessions
Killing Oracle Sessions
Killing Oracle Sessions
ThereareanumberofwaystokillroguesessionsbothwithinOracleandexternally.
Relatedarticles.
IdentifytheSessiontobeKilled
Killingsessionscanbeverydestructiveifyoukillthewrongsession,sobeverycarefulwhenidentifyingthe
sessiontobekilled.Ifyoukillasessionbelongingtoabackgroundprocessyouwillcauseaninstancecrash.
Identifytheoffendingsessionusingthe[G]V$SESSIONand[G]V$PROCESSviewsasfollows.
SETLINESIZE100
COLUMNspidFORMATA10
COLUMNusernameFORMATA10
COLUMNprogramFORMATA45
SELECTs.inst_id,
s.sid,
s.serial#,
p.spid,
s.username,
s.program
FROMgv$sessions
JOINgv$processpONp.addr=s.paddrANDp.inst_id=s.inst_id
WHEREs.type!='BACKGROUND';
INST_IDSIDSERIAL#SPIDUSERNAMEPROGRAM
[email protected]
(TNSV1V3)
[email protected]
(TNSV1V3)
[email protected]
(J000)
[email protected]
(J001)
SQL>
TheSIDandSERIAL#valuesoftherelevantsessioncanthenbesubstitutedintothecommandsinthe
followingsections.
ALTERSYSTEMKILLSESSION
Thebasicsyntaxforkillingasessionisshownbelow.
SQL>ALTERSYSTEMKILLSESSION'sid,serial#';
InaRACenvironment,youoptionallyspecifytheINST_ID,shownwhenqueryingtheGV$SESSIONview.This
allowsyoutokillasessionondifferentRACnode.
SQL>ALTERSYSTEMKILLSESSION'sid,serial#,@inst_id';
TheKILLSESSIONcommanddoesn'tactuallykillthesession.Itmerelyasksthesessiontokillitself.Insome
situations,likewaitingforareplyfromaremotedatabaseorrollingbacktransactions,thesessionwillnotkill
itselfimmediatelyandwillwaitforthecurrentoperationtocomplete.Inthesecasesthesessionwillhavea
statusof"markedforkill".Itwillthenbekilledassoonaspossible.
Inadditiontothesyntaxdescribedabove,youcanaddtheIMMEDIATEclause.
SQL>ALTERSYSTEMKILLSESSION'sid,serial#'IMMEDIATE;
Thisdoesnotaffecttheworkperformedbythecommand,butitreturnscontrolbacktothecurrentsession
immediately,ratherthanwaitingforconfirmationofthekill.
Ifthemarkedsessionpersistsforsometimeyoumayconsiderkillingtheprocessattheoperatingsystemlevel.
Beforedoingthisit'sworthcheckingtoseeifitisperformingarollback.Youcandothisbyrunningthisscript
(session_undo.sql).IftheUSED_URECvalueisdecreasingforthesessioninquestionyoushouldleaveitto
completetherollbackratherthankillingthesessionattheoperatingsystemlevel.
ALTERSYSTEMDISCONNECTSESSION
TheALTERSYSTEMDISCONNECTSESSIONsyntaxisanalternativemethodforkillingOraclesessions.Unlikethe
KILLSESSIONcommandwhichasksthesessiontokillitself,theDISCONNECTSESSIONcommandkillsthe
dedicatedserverprocess(orvirtualcircuitwhenusingSharedSever),whichisequivalenttokillingtheserver
processfromtheoperatingsystem.ThebasicsyntaxissimilartotheKILLSESSIONcommandwiththeaddition
ofthePOST_TRANSACTIONclause.TheSIDandSERIAL#valuesoftherelevantsessioncanbesubstitutedinto
oneofthefollowingstatements.
SQL>ALTERSYSTEMDISCONNECTSESSION'sid,serial#'POST_TRANSACTION;
SQL>ALTERSYSTEMDISCONNECTSESSION'sid,serial#'IMMEDIATE;
ThePOST_TRANSACTIONclausewaitsforongoingtransactionstocompletebeforedisconnectingthesession,
whiletheIMMEDIATEclausedisconnectsthesessionandongoingtransactionsarerolledbackimmediately.
ThePOST_TRANSACTIONandIMMEDIATEclausescanbeusedtogether,butthedocumentationstatesthatin
thiscasetheIMMEDIATEclauseisignored.Inaddition,thesyntaxdiagramsuggestsbothclausesareoptional,
butinreality,oneorbothmustbespecifiedoryoureceiveanerror.
SQL>altersystemdisconnectsession'30,7';
altersystemdisconnectsession'30,7'
*
ERRORatline1:
ORA02000:missingPOST_TRANSACTIONorIMMEDIATEkeyword
SQL>
Thiscommandmeansyoushouldneverneedtoswitchtotheoperatingsystemtokillsessions,whichreduces
thechancesofkillingthewrongprocess.
TheWindowsApproach
TokillthesessionontheWindowsoperatingsystem,firstidentifythesession,thensubstitutetherelevantSID
andSPIDvaluesintothefollowingcommandissuedfromthecommandline.
C:\>orakillORACLE_SIDspid
Thesessionthreadshouldbekilledimmediatelyandallresourcesreleased.
TheUNIXApproach
Warning:IfyouareusingtheMultithreadedModelinOracle12c,youshouldnotattempttokilloperating
systemprocesses.Toknowwhy,readthis.
TokillthesessiononUNIXorLinuxoperatingsystems,firstidentifythesession,thensubstitutetherelevant
SPIDintothefollowingcommand.
%killspid
Ifafterafewminutestheprocesshasn'tstopped,terminatethesessionusingthefollowing.
%kill9spid
IfindoubtcheckthattheSPIDmatchestheUNIXPROCESSIDshownusing.
%psef|grepora
Thesessionthreadshouldbekilledimmediatelyandallresourcesreleased.
Formoreinformationsee:
Hopethishelps.RegardsTim...