Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SAPNetWeaverASABAPRelease702,Copyright2010SAPAG.Allrightsreserved.
ABAPKeywordDocumentationABAPReferenceProcessingExternalDataDataConsistency
DatabaseLocking
Anydatabasepermittingsimultaneousaccessbyseveraltransactionsrequiresdatabaselockstomanageandsynchronize
access.Thetasksofthismechanismareto:
Protectdataobjectsthatatransactioniscurrentlychangingorreadingfrombeingchangedbyothertransactionsatthe
sametime.
Protectatransactionfromreadingdataobjectsthathavenotyetbeenfullywrittenbackbyanothertransaction.
Example:Inaflightreservationsystem,supposeyouwanttomakeabookingforLufthansaflight0400on16.05.1996.Thisis
onlypossibleiftherearestillenoughfreeseats.Topreventtwobookingsfrombeingmadeatthesametimeandthusavoid
overbooking,theentryinthedatabasetableSFLIGHTcorrespondingtothisflightmustbeprotectedagainstchangesbyother
transactions.ThisensuresthatthequeryaboutthenumberoffreeseatsinthefieldSEATSOCC,thebookingoftheflight,and
theupdateofthefieldSEATSOCCcanproceedundisturbedbyothertransactions.
Howislockingachieved?
Databasesystemsdonotusuallyprovidecommandsforexplicitlysettingorreleasinglocks.Therefore,priortoexecutingthe
databaseoperation,databaselocksaresetimplicitlywhenoneoftheOpenSQLstatementsSELECT,INSERT,UPDATE,
MODIFY,DELETE(orthecorrespondingNativeSQLstatement)iscalled.
Whatislocked?
Databasesystemssetphysicallocksonalllinesaffectedbyadatabasecall.InthecaseofSELECT,thesearetheselected
entries.InthecaseofUPDATE,DELETE,INSERTandMODIFY,theyaretheentriestobechanged,deleted,andsoon.
Forexample,thefollowingcalllockstheentryinthetableSFLIGHTfortheLufthansaflight0400on16.05.1996.
SELECTSINGLEFORUPDATE*FROMsflight
WHERE
CARRID='LH'AND
CONNID='0400'
FLDATE='19960516'.
Itisnotalwaysthetablelinethatislocked.Tables,datapages,andindexpagescanalsobelocked.Theunitstobelocked
dependonthedatabasesystemyouareusingandtheaccessbeingperformed.
Lockmode
Inprinciple,onetypeoflockisenoughtocontrolconflictingdataaccesses.However,toachieveagreaterdegreeofparallel
runningamongtransactions,databasesystemsuseseveraltypesoflocks.Thesecanvaryfromsystemtosystem,butthe
followingtwoaresufficienttogainanunderstandingofhowlockingworks:
Readlock(sharedlock)
Readlocksallowthesystemtosetotherreadlocks,butpreventothertransactionsfromsettingwritelocksforthe
objectsinquestion.
Writelock(exclusivelock)
Writelocksdonotallowothertransactionstosetanylocksfortheobjectsinquestion.
Howarelocksset?
YousetwritelockswiththeOpenSQLstatementsSELECTSINGLEFORUPDATE,INSERT,UPDATE,INSERT,UPDATE,
MODIFYandDELETE(orwiththeappropriateNativeSQLstatements).
http://help.sap.com/abapdocu_702/en/abendb_lock.htm 1/3
9/12/2016 ABAPKeywordDocumentation
ThedecisionastowhetherornottheOpenSQLcommandSELECT(ortheappropriateNativeSQLcommand)setsthelock
dependsontheisolationlevelofthetransaction.Therearetwopossibleisolationlevels:
Uncommittedread(ordirtyread)
Aprogramusingan"uncommittedread"toreaddatadoesnotsetlocksondataobjects.Forthisreason,programmers
mustbearinmindthattheirprogramsmightreaddatathatisstillprotectedbyawritelockandhasnotyetbeenfinally
writtentothedatabasewithadatabasecommitandcouldthusstillbedeletedfromthedatabasebyadatabase
rollback."uncommittedread"isthedefaultsettingfortheisolationlevelinASABAP.(Exception:Oracledatabasesdo
notsupportuncommittedreadscommittedreadisalwayssetinthesedatabases.)
CommittedRead
Aprogramusinga"committedread"toreaddatasetsareadlockonadataobject,readsit,andimmediatelyreleases
thelock.Thismeansthatprogrammerscanbesurethattheirprogramswillreadonlydatathatisnotprotectedbya
writelockandthathasbeenfinallywrittentothedatabasewithadatabasecommit.Youcansettheisolationlevelin
the
Manydatabasesystemsemployadditionalisolationlevels(suchas"cursorstability"and"repeatableread").Theseworklike
"committedread",butthereadlockisretaineduntilthenextdataobjectisreadoruntilthedatabasecursorisclosed.Since
theseisolationlevelsarenotsufficientlystandardized,theyarenotcurrentlyusedinASABAP.
Ifatransactioncannotlockanobjectbecauseitisalreadylockedbyanothertransaction,itwaitsuntiltheothertransactionhas
releasedthelock.Thiscanresultinadeadlock.Adeadlockoccurs,forexample,whentwotransactionsarewaitingforalock
heldbytheother.
Thefollowingprogramfragmentdemonstratesasolutiontothisproblem:
DATAsflight_waTYPEsflight,sbook_watypesbook.
SELECTSINGLE*FORUPDATEFROMSFLIGHT
INTOSFLIGHT_WA
WHERE
CARRID='LH'AND
CONNID='0400'AND
FLDATE='19960516'.
IFsysubrc<>0.
MESSAGEE...
ENDIF.
IFsflight_waseatsocc<sflight_waseatsmax.
SBOOK_WACARRID='LH'.
SBOOK_WACONNID='0400'.
SBOOK_WAFLDATE='19960516'.
...
INSERTSBOOKFROMSBOOK_WA.
IFsysubrc<>0.
MESSAGEE...
ENDIF.
UPDATESFLIGHT
SET
SEATSOCC=SEATSOCC+1
WHERE
CARRID='LH'AND
CONNID='0400'AND
FLDATE='19960516'.
ELSE.
MESSAGEE...
ENDIF.
COMMITWORK.
http://help.sap.com/abapdocu_702/en/abendb_lock.htm 2/3
9/12/2016 ABAPKeywordDocumentation
ThetablerowselectedbySELECTSINGLEFORUPDATEandinsertedbyINSERTislockeduntiltheendofthedatabase
LUW.ThispreventsbothoverbookingtheflightandanyinconsistencybetweenthetablesSFLIGHTandSBOOKintheevent
ofadatabaserollbackafteranerror.
Howlongisalockretained?
Indatabaselocking,alllocksarereleasednolaterthanthenextdatabasecommitorrollback.Readlocksareusuallyretained
forashorterperiod.Sometimes,thiscausesproblemsfortransactionsthatcoverseveraldialogsteps:
Aftertheuserhasselectedaflightintheaboveexample,heorsheusuallyperformsfurtherdialogstepstoenteradditional
dataforthereservation.Here,theflightreservationisaddedinadifferentdatabaseLUWthantheoriginalselectionofthe
flight.Databaselockingdoesnotpreventanothertransactionfrombookingthisflightinthemeantime.Thismeansthatthe
scheduledbookingmayhavetobecanceledafterall.
Fromtheuser'spointofview,thissolutionisveryinconvenient.Toavoidthisscenario,aflightreservationsystemmustusethe
SAPlockingmechanismtolocktheflightfortheentiredurationofthetransaction.
Thistranslationdoesnotreflectthecurrentversionofthedocumentation.
Continue
LockConflictsinIBMDB2
http://help.sap.com/abapdocu_702/en/abendb_lock.htm 3/3