Lock objects are used to provide logical locking functionality in programs. Say, if two users should not edit the same document simultaneously, then we can implement lock objects to restrict one user from opening the document in case another user alredy opened it for editing.
When lock object is defined in ABAP Dictionary, two function modules ENQUEUE_obj_name
and DEQUEUE_obj_name
are created. They can be afterwards called in ABAP programs to respectively lock and unlocks the objects.
In lock object itself in Tables
tab we define the primary table which we want the lock for. In Lock parameters
tab we define the parameters which are passed to the function module during requests to set or release the lock and which correspond to table fields acting as a lock criteria condition.
Let's say we create a lock EZDOC_BILLING for a Billing Document header table VBRK. As lock parameters by default key fields of the table MANDT and VBELN are automatically populated.
Upon lock object activation function modules ENQUEUE_EZDOC_BILLING
and DEQUEUE_EZDOC_BILLING
are generated.
To implement it in the code we call the enqueue FM with a document number for which lock should be set. Once it's successfully done we can proceed with our operations like changing the document and the locking ensures that no other user can edit the same document in this time. Once we're done we should relese the lock by calling the dequeue FM.
In general it can look like this in the coding:
...
PERFORM set_lock USING l_vbeln CHANGING l_retcode.
IF l_retcode = 0.
PERFORM change_doc_billing ...
ENDIF.
PERFORM release_lock USING l_vbeln.
...
FORM set_lock USING i_vbeln CHANGING c_retcode.
CALL FUNCTION 'ENQUEUE_EZDOC_BILLING'
EXPORTING
mode_vbrk = 'E'
mandt = sy-mandt
vbeln = i_vbeln
_scope = '2'
_wait = ' '
_collect = ' '
EXCEPTIONS
foreign_lock = 1
system_failure = 2
OTHERS = 3.
c_retcode = sy-subrc.
ENDFORM.
FORM release_lock USING i_vbeln.
CALL FUNCTION 'DEQUEUE_EZDOC_BILLING'
EXPORTING
mode_vbrk = 'E'
mandt = sy-mandt
vbeln = i_vbeln
x_vbeln = ' '
_scope = '3'
_synchron = ' '
_collect = ' '.
ENDFORM.