A Lock Object is an essential tool in SAP ABAP for ensuring data consistency by controlling access to database tables. When multiple users attempt to access the same data at the same time, a Lock Object can be used to prevent conflicting changes and ensure that data is updated in a consistent manner.
To implement a Lock Object in SAP ABAP, the following steps are required:
- Define a Lock Object by creating a new entry in the SAP Lock Management (SM12) transaction.
- The Lock Object requires a unique name and the table(s) that it applies to should be defined.
- Within the ABAP code, use the ENQUEUE function module to lock the record or object before accessing it, and the DEQUEUE function module to release the lock after access is complete.
- Set a lock mode for the lock object to determine how restrictive the lock should be.
- The lock mode can be set to shared or exclusive, and it can also be set to timeout after a specified period.
Types of Lock Objects:
Exclusive (E) or Read mode: The locked data can only be displayed or modified by single user i.e. the owner of the object. Access to other users is denied.
Shared (S) or Write Mode: Several users can access the same record simultaneously, but only in display mode and except the first one, who has asked for the data in update mode.
Exclusive not cumulating (X) it is similar to exclusive lock. It allows only a single user access. E can be called several times from the same transaction. In contrast, a lock type X can be called only once during the transaction. Any other call for this lock is rejected.
In summary, Lock Objects are crucial for maintaining data consistency and preventing conflicts when multiple users try to access the same data. They can be implemented using the ENQUEUE and DEQUEUE function modules and can be set to different lock modes. SAP ABAP has two types of Lock Objects: SAP Enqueue Lock Object and SAP Lock Object.
When ever we create a lock object two function modules will be created, go to SE37, check FM`s DEQUEUE_EZSTUDENT and ENQUEUE_EZSTUDENT.
Using the lock object to update/insert table
When ever we create a lock object, two function modules(ENQUEUE and DEQUEUE) will be created, we have to call those function modules before and after open SQL statements, refer program below.
Below is a sample program in for the same , which can be called from se80 Reports
REPORT ZSAN_LOCKOBJECT.
DATA : IT_STUDENT TYPE TABLE OF ZSTUDENT,
WA_STUDENT TYPE ZSTUDENT.
WA_STUDENT-STUDENTID = '09'.
WA_STUDENT-NAME = 'SAPNuts'.
WA_STUDENT-LASTNAME = 'SAPNuts'.
WA_STUDENT-FATHER = 'SAP'.
WA_STUDENT-DOB = '25/09/2013'.
WA_STUDENT-GENDER = 'Male'.
WA_STUDENT-COURSE = 'SAP ABAP'.
WA_STUDENT-EMAIL = 'admin@sapnuts.com'.
WA_STUDENT-FEE = '0000'.
CALL FUNCTION 'ENQUEUE_EZSTUDENT' "add lock
EXPORTING
MODE_ZSTUDENT = 'E'
MANDT = SY-MANDT
STUDENTID = WA_STUDENT-STUDENTID "record to lock, optional parameter
* X_STUDENTID = ' '
* _SCOPE = '2'
* _WAIT = ' '
* _COLLECT = ' '
* EXCEPTIONS
* FOREIGN_LOCK = 1
* SYSTEM_FAILURE = 2
* OTHERS = 3
.
IF SY-SUBRC = 0.
MODIFY ZSTUDENT FROM WA_STUDENT.
ENDIF.
CALL FUNCTION 'DEQUEUE_EZSTUDENT' "release lock
EXPORTING
MODE_ZSTUDENT = 'E'
MANDT = SY-MANDT
STUDENTID = WA_STUDENT-STUDENTID "record to release lock, optional parameter
* X_STUDENTID = ' '
* _SCOPE = '3'
* _SYNCHRON = ' '
* _COLLECT = ' '