|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
楼上说交互性不好,太牵强了吧。在微软提供的一套框架中,利用asp做网站,开发效率高,使用人数少,减少不必要的开销。交互性是互动方式,是有开发人员决定的。事件处置|数据|数据库ASPTransactions
byChrisPayneIntroduction
Transactionsareimportanttomaintaindataintegrity,amongotherthings,andhavebeenusedwith
databasesforsometimenow.Luckily,transactionsarentrestrictedtodatabases-youcanusethemin
ActiveServerPagesaswell,andwithouthavingtocreatecustomcomponentsusingMicrosoftTransaction
Server(MTS).Thisarticlewilltakealookatwhattransactionsare,howtheywillhelpASPdevelopers,
andhowtoimplementthem.
WhatareTransactions?
Beforewediveintothemeat,wehavetounderstandtheterminology(believeme,Iwaslostforalong
timebecausenooneexplainedthesetermstome).
Basically,usingatransactionmeansitsanall-or-nonedeal.Nohalfways,nomaybes.Letssuppose
youremodifyingadatabase,andyouhavetoadd100newitems,oneatatime.Beforeaddingeachitem,
however,youhavetochecktomakesureitfitscertaincriteria-ifitdoesnt,thenyourdatabase
cries,youreceiveanerror,andtheprocessquits.Itmaynotbedesirabletoquithalfway-forwhatever
reason,youeitherwantallthenewrowsornoneofthem.
Entertransactions.Ifyouputtheprocessinatransaction,andifanerroroccurs,thetransaction
will"rollback"toastatejustbeforeyoustartedyourprocess.Thus,ifanyofthenewitemsdoesnt
meetthecriteria,noneofitemsgetsadded,insteadofonlythefirstfew.
Theclassicexampleisabankingapplication.Supposesomeonewantstotransferfundsfromoneaccountto
another.YouwoulddeductxamountfromaccountA,andthenaddittoaccountB.Supposeafteryouhave
deductedthemoneyfromaccountA(andbeforeyouaddittoaccountB),younoticeaproblem(perhaps
accountBhasrestrictions,orisnotavailableforsomereason).Iftheapplicationjustquitsherewith
someerrormessage,thenaccountAwillbeinanincorrectstate,meaningithasfundsmissing.These
fundsarenotinaccountByet,sotheyarejustfloatingoffincyberspace,andyouhaveoneveryunhappy
customer.Withtransactions,shouldyouencounterthiserror,theprocesswillrollbacktoaprevious
state,andthemoneywillautomaticallygobacktowhereitstarted.Iftherearenoproblems,however,
thetransactionwillcommit,andeverythingwillgothrough.
Anotherexamplemoreapropostowebdevelopmentisuserregistration.Supposeausercomestoyour
communitybuildingwebsiteanddecidestoregister.Oncetheysubmittheirinformation,youhavetoenter
itintoadatabase,andalsosetuptheirpersonalwebpage.Thus,everyuserinthedatabasemusthavea
personalwebpageaswell,ornoonewilleverknowthattheyarethere.YourASPscriptaddstheuser
intothedatabaseperfectly,butaservererrorcausesyourwebpagebuildingroutinetofail
unexpectedly.Younowhaveafloatinguser-auserinthedatabase,butwithoutawebpage.Ifthis
processweretransactional,youcouldrollbacktojustbeforeyouinsertedtheuserintothedatabase
(therebyeliminatingthefloatinguserproblem),andgracefullydisplayaniceerrormessagetotheuser.
Prettyslick,huh?
SoHowDoesitHelpMe?
Itsalwaysgottobeabout"me,"doesntit?Well,ifyoudidntgatheritfromabove,transactionsare
mainlyusedforerrorhandling.Theystopyoufromgettinginweirdpositionsthatyoucantgetoutofby
easilyallowingyoutoundothechangesyouvemade.Asyoudevelopmoreandmorecomplexweb
applications,theneedforstrongstateprotectionwillbecomeincreasinglynecessary.
WhatisMTS?
MTSisMicrosoftsanswertotransactions.Wewontgointotoomuchdetailhere,butMTSistheengine
thatkeepstrackofobjectstates,androllsthembackorcommitsthemwhennecessary.Eventhoughit
wontbeapparentinyourASPs,MTSisworkingbusilybehindthescenestokeeptrackofwhatyoure
doing.
MTSrequiresallobjectsthatusetransactionstoberegisteredinMTS.IISitselfhasmanydifferent
componentsthatprocesswebapplicationsthatareallregisteredwithMTS,whichiswhyASPscanrun
transactionallythroughMTS,invisibletotheuser(andsometimestothedeveloper).Sojusttrustuswhen
wesayyouneedMTS.
NOTE:ThoughMTSisinstalledwithWindowsNT,itusuallyisnotsetuptorunautomatically.Therefore,
beforerunninganyoftheexamplesbelow,makesuretheMTSservice(MSDTC)isrunning(intheservices
contr</p>想法是和程序员的想法不一样的.至于为什么.大家去想一想.跟心理学有关的 |
|