You are on page 1of 3

16/8/2016 oracle - Polling a database versus triggering program from database?

- Stack Overow

signup login tour help

Dismiss

AnnouncingStackOverflowDocumentation
WestartedwithQ&A.Technicaldocumentationisnext,andweneedyourhelp.
Whetheryou'reabeginneroranexperienceddeveloper,youcancontribute.

Signupandstarthelping LearnmoreaboutDocumentation

Pollingadatabaseversustriggeringprogramfromdatabase?

IhaveaprocesswhereinaprogramrunninginanapplicationservermustaccessatableinanOracledatabaseserverwheneveratleastone
rowexistsinthistable.Eachrowofdatarelatestoaclientrequestingsomenumbercrunchingperformedbytheprogram.Theprogramcan
onlyperformthisnumbercrunchingserially(thatis,foroneclientatatimeratherthanmultipleclientsinparallel).

Thus,theprogramneedstobeinformedofwhendataisavailableinthedatabaseforittoprocess.Icouldeither

1.havetheprogrampollthedatabase,or
2.havethedatabasetriggertheprogram.

QUESTION1:Isthereanyconventionalwisdomwhyoneapproachmightbebetterthantheother?

QUESTION2:Iwonderifprogramshaveanyissues"running"formonthsatatime(wouldanyprocessesintheserverstopordisruptthe
programfromrunning?ifsoIdon'tknowhowI'dlearntherewasaproblemunlessfromangrycustomers).Anyonehaveexperiencerunning
programsonaserverforalongtimewithoutissues?Or,iftheserverdoescrash,isthereawaytoautostarta(i.e.Clanguageexecutable)
programonitaftertheserverreboots,thusnotrequiringahumantostartitspecifically?

Anyadviceappreciated.

UPDATE1:Clientiswaitingforresults,butacouplesecondsadditionaldelay(frompolling)isn'tadealbreaker.

database oracle

editedMar14'12at22:32 askedMar14'12at21:57
Ghost ggkmath
6,957 2 11 36 1,895 10 48 103

1 FarasI'mconcerned,"dependingonhowwelltheprogramiswritten"shouldbe"iftheprogramiswritten
correctlyornot".Amemoryleakisabug.Youshouldn'tmakeyourdesigndecisionsonthebasisof
potentialbugs.CodeBlingMar14'12at22:03

2)makesnosense.There'snosuchthingasa"Cprogram"therearejustprograms.Cisalanguagein
whichyoucanauthoraprogram,butoncecompiled,aprogramisaprogram.IfyourOScanrunprograms,
thenitcanrun"Cprograms",too,andifittellsyouitwillkillthemafter30minutes,thenit'llkillthemno
matterwhetherthey'rewritteninCornot.Thatsaid,ifyouwriteterriblecode,youcanmakeprogramsdie
aftertheyrunacertaintimebyconsumingtoomanyresources.KerrekSBMar14'12at22:03

@KerrekjusteditedtoremovereferencetoCsincethequestionisn'treallylanguagespecificCodeBling
Mar14'12at22:06

OK,perhapsIfocusedtoomuchonthefactthatI'mcodinginC.ThankyouCodeBlingfortheedits.The
questionboilsdownto,"isitbettertorunaprogramthatcontinuouslypollsadatabase,orhavethe
databasetriggertheexecutionoftheprogram?" ggkmath Mar14'12at22:08

ggkmath,Iguessitdependsonhowfrequenttheupdatestothedatabasemaybe,butyouitdoesn't
necessarilyhavetoruncontinously.Youhavetoweightheadvantagesofpollingvseventdriven.Usually
eventdrivenismoreresponsiveandlightweight(memoryandCPU),butthereareothercosts.Forone
thing,maintainingtwolinkedcodebasesindifferentlanguages(oneisyourmainprogram,theotherrunson
thedatabase,usually)thatareinterdependentcanbeanightmare.Allthatbeingsaid,I'msureyou'llget
somegreatactualanswersbelow!:)CodeBlingMar14'12at22:25

5Answers

1)havetheprogrampollthedatabase,sinceyoudon'twantyourdatabasetobeableto
starthostprograms(becauseyou'dhavetomakesurethatonly"your"programcanbestarted
thisway).

Theclassic(andmostconvenientIMO)wayfordoingthisinOraclewouldbethroughthe
DBMS_ALERTpackage.
http://stackoverow.com/questions/9710910/polling-a-database-versus-triggering-program-from-database 1/3
16/8/2016 oracle - Polling a database versus triggering program from database? - Stack Overow
Thefirstprogramwouldsignalanalertwithacertainname,passinganoptionalmessage.A
secondprogramwhichregisteredforthealertwouldwaitandreceiveitimmediatlyafterthe
firstprogramcommits.Arollbackofthefirstprogramwouldcancelthealert.

Ofcauseyoucanhavemanysessionssignalingandwaitingforalerts.However,analertisa
serializationdevice,soifoneprogramsignaledanalert,otherprogramssignalingthesame
alertnamewillbeblockeduntilthefirstonecommitsorrollsback.

TableDBMS_ALERT_INFOcontainsallthesessionswhichhaveregisteredforanalert.You
canusethistocheckifthealertprocessingisalive.

2)autostartingorbackgroundexecutiondependsonyourhostplatformandOS.InWindows
youcanuseSRVANY.EXEtorunanyexecutableasaservice.

editedMar14'12at23:43 answeredMar14'12at23:26
HAL9000
2,615 1 11 22

IrecommendusingaCprogramtopollthedatabaseandautilitysuchasmonittorestartthe
Cprogramifthereareanyproblems.YourCprogramcantouchafileonceinawhileto
indicatethatitisstillfunctioningproperly,andmonitcanmonitorthefile.Monitcanalsocheck
theprocessdirectlyandmakesureitisn'tusingtoomuchmemory.

Formoreinformationyoucouldseemyanswerofthisotherquestion:

Whenanewrowindatabaseisadded,anexternalcommandlineprogrammustbeinvoked

Alternatively,ifpeoplearen'tsittingaroundwaitingforthecomputationtofinish,youcoulduse
acronjobtoruntheCprogramonaregularbasis(e.g.everyminute).Thenmonitwouldbe
lessneededbecauseyourCprogramwillstartandstopallthetime.

answeredMar14'12at22:02
DavidGrayson
38.9k 10 70 114

ThanksDavid,inmycasetheclientiswaitingfortheresults.YourlinkisstatingbasicallywhatI'mtryingto
do.TheCprogramwillprocessthefirstrow,thendeletethisrowwhendone,makingtheprevious2ndrow
nowthefirstrow,whichtheCprogramwillthenprocess,etc.Iwasgoingtohavetheprogramupdatesome
fieldinthedatabaseperiodically(e.g.aheartbeatfield)justtoshowit'srunningfine,butusinganexternalfile
shouldalsowork. ggkmath Mar14'12at22:18

Thanksforthemonitlink. ggkmath Mar14'12at22:36

Yeah,afileisnicebecauseyoudon'thavetochangeyourdatabaseschema,anditcanbeeasilymonitored
bymonit.DavidGraysonMar14'12at22:43

Therearesimplejobmanagerslikegearmanthatyoucanusetosendajobmessagefromthe
databasetoaworker.GearmanhasamongothersaMySQLuserdefinedfunctioninterface,
soitisprobablyeasytobuildonefororacleaswell.

answeredMar14'12at22:24
ErikEkman
1,835 6 12

YoumightwanttolookintoOracle's"ChangeNotification":

http://docs.oracle.com/cd/E11882_01/appdev.112/e25518/adfns_
cqn.htm

Idon'tknowhowwellthisintegrateswitha"regular"Cprogram
though.

It'salsoavailablethrough.NetandJava/JDBC

http://docs.oracle.com/cd/E11882_01/win.112/e23174/featChang
e.htm

http://stackoverow.com/questions/9710910/polling-a-database-versus-triggering-program-from-database 2/3
16/8/2016 oracle - Polling a database versus triggering program from database? - Stack Overow
http://docs.oracle.com/cd/E11882_01/java.112/e16548/dbchgnf.ht
m

answeredMar14'12at22:42
a_horse_with_no_name
177k 24 220 296

Iwouldliketogiveamoregenericanswer...

Thereisnorightanswerthatapplieseverytime.Sometimesyouneedatrigger,andsome
timesisbettertopoll.

But9outof10times,pollingismuchmoreefficient,safeandfastthantriggering.

It'sreallysimple.Atriggerneedstoinstantiateasingleprogram,ofwhatevernature,forevery
shot.Thatisjustnotefficientmostofthetime.Somepeoplewillarguethatthatisrequired
whenresponsetimeisafactor,buteventhen,halfofthetimespollingisbetterbecause:

1)Resources:Withtriggers,andsay100messages,youwillneedresourcesfor100threads,
with1threadprocessingapacketof100messagesyouneedresourcesfor1program.

2)Monitoring:Athreadprocessingpacketscanreporttimeconsumedconstantlyonadefined
packetsize,clearlyindicatinghowitisperformingandwhenandhowisperformancebeing
affected.Trythatwithabilliontriggersjumpingaround

3)Speed:Instantiatingthreadsandallocatingtheirresourcesisveryexpensive.Anddontget
mestartedifyouareopeningatransactionforeachtrigger.Asimpleprogramprocessinga
say100meessagepacketwillalwaysbemuchfasterthatinitiating100triggers

3)Reactiontime:Withpollingyoucannotreacttothingsonline.So,theonlyexception
allowedtousepollingiswhenauseriswaitingforthemessagetobeprocessed.Butthenyou
needtobeverycareful,becauseifyouhavelotsofclientsdoingthesamethingatthesame
time,triggeringmightrespondLATER,thanifyouwheredoingfastpolling.

My2cts.Thishasbeenlearnedthehardway..

editedAug16'12at18:35 answeredAug16'12at18:30
AlexVaz
366 2 6

http://stackoverow.com/questions/9710910/polling-a-database-versus-triggering-program-from-database 3/3

You might also like