资源描述:
《Instroduction to Transaction.pdf》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、IntroductiontoJavaTransactionAPIAtransactionisaseriesofoperationsthatmustbeperformedatomically.Inotherwords,eachoperationintheseriesmustsucceedfortheentiretransactiontobesuccessful.Ifanyoperationinthetransactiondoesnotsucceed,theentiretransactionfails.Atthatpoint,anyoperationsw
2、hichhavesucceededmustbe"rolledback"sothattheendstatematcheswhatwasinplacebeforethetransactionstarted.Forexample,let'ssayyouwanttotransfer$50fromasavingsaccount(accountnumber:12345-1)toacheckingaccount(accountnumber12345-2).Thestepsinthistransactioncanbestatedin"pseudocode"asfol
3、lows:BOUNDARY:STARTTRANSACTIONASUBTRACT50DOLLARSFROMSAVINGSACCOUNT12345-1ADD50DOLLARSTOTHECHECKINGACCOUNT12345-2BOUNDARY:ENDTRANSACTIONATorunthetransaction,thefollowingcodeisrequired:PREPARE(RUN)TRANSACTIONAIFTRANSACTIONASUCCEEDEDCOMMITTRANSACTIONAELSEROLLBACKTRANSACTIONALet'sa
4、ssumethattherearesufficientfundstosubtract$50fromthesavingsaccount,sothefirstpartofthetransactionsucceeds.However,let'salsoassumethatthesecondpartfails.Whenthecomputerattemptstoadd50dollarstothecheckingaccount,itdiscoversthatthecheckingaccountisfrozen.Becausethesecondpartofthet
5、ransactionfails,theentiretransactionfails.Asaresult,thefirstpartofthetransactionneedstoberolledback:$50needstobeplacedbackintothesavingsaccount12345-1.Thisisessential.Otherwise,eachtimethecomputertriestotransfermoneyfromthesavingstothecheckingaccount,itwilllosethemoney!Iftheent
6、iretransactionsucceeds,thentheentiretransactioniscommitted,andtheresultsaremadepermanent.Notethatthecommitprocesstakestwophasestocomplete.Inthefirstphase,checksaremadetoseethatthetransactionranwithouterror.Iftherewerenoerrors,theresultsarecommittedinthesecondphase.Iftherewereer
7、rorsinthefirst-phasecheck,thetransactionisrolledback.Thiscommontransactionstrategyisappropriatelycalledthetwo-phasecommitprotocol.TransactionsintheJ2EEEnvironmentTherearetypicallythreeparticipantsinatransaction:anapplicationthatinitiatestherequestforthetransaction,adatastore(su
8、chasadatabase)thatthetransactionrunsagainst,andanAPI(s