Lock Objects in SAP ABAP

Let us understand Lock Objects in SAP ABAP from a real life scenario. Have you ever booked a seat in a bus or a seat for movie online? If yes, you might have faced a situation where you were doing the booking and the seat got locked, it means the seat will be locked permanently on your name if you pay for it or will be unlocked after certain time period if you denied payment. This is an example of table lock, where the same seat cannot be booked by two different people. This feature in SAP ABAP is called Lock Objects.

Below is the online portal of Movie tickets seat selection page.

Lock Objects in SAP ABAP-Movie Hall Lock Seat

Here the red seats are the permanently locked seat; green seats are the one which is locked temporarily. Lock objects locks a table temporarily till the operation is performed on the table.

Definition

Lock Objects in SAP ABAP are global reusable component which generates function modules that are used to set and release locks on data record. This lock mechanism is important to synchronize access of same data record by multiple programs/users.

Function Modules Generated by Lock Objects

  1. ENQUEUE_<lock object name>: It sets the lock.
  2. (DEQUEUE_<lock object name>: It releases the lock.

*NOTE: These FMs are automatically assigned to a Function Group; these FMs are generated every time, when you activate the locks. While transporting these locks, do not transfer either FMs or Function Group, just transfer the lock object.

Creation of Lock Objects in SAP ABAP:

Step 01: Open SE11.

Step 02: Click Lock Objects Radio Button from the given radio buttons.  Give your Lock Objects name starting with EZ or EY e.g. “EZBarry_lock”.

Note: The name of your lock object must begin with E. Here E defines Enqueue.

Step 03: Click Create, Enter explanatory description for Lock Objects in Short Description e.g. “Lock Objects for Test”.

Step 04: In the Attributes section, you can maintain the following:

Field Value Description
Package Your Package name Enter your package name, in this way all the FMs and FG will be also transferred here.
Allow RFC Checked/ Unchecked If you Allow RFC, the generated FMs can be called from another system with Remote Function Call.

 

*Note: If you enable the RFC for an existing Lock, before activating the lock object, you must ensure that the generated FMs that were called by your ABAP Programs are having the appropriate Parameters.

Step 05: In the Tables section, you can maintain the following:

Field Value Description
Primary Table: The table that will be Primarily have the lock.
Name Name of the table It is the Primary table or the main table which is being locked
Lock Mode Select one of the option from Drop Down It controls how the lock affects table and how it is passed to the reports/user
Secondary Table: The table that is linked with Primary table via Foreign keys relationship.
Name Name of the table It is the table which is linked to Primary table via Foreign relationship.
Lock Mode Select one of the option from Drop Down It controls how the lock affects table and how it is passed to the reports/user

Lock Mode: On a single table, multiple users can set their own lock by creating their own Lock Objects, the given lock mode enables one or all of them access over the table at the same time.

Type of Lock Lock Mode Description
Shared Lock S ( S hared) It means several users/transactions can access locked data at the same time in display mode : Read lock
Exclusive Lock E ( E xclusive) It means the user who has locked data, only he can unlock it  : Write lock
Exclusive but not cumulative lock X (e X clusive non-cumulative) Two or more different lock objects can lock a table simultaneously. Once the Lock is freed by first lock object, it will be locked by second Lock object. In this case, one lock object cannot lock a table more than once simultaneously.
Optimistic Lock O ( O ptimistic) In case more than one user is using lock for same table, but not all are changing data, it’s not good to leave table locked. An Optimistic Lock changes in to E(Exclusive) mode as soon as the user tries to save the data.

 

Step 06: In the Lock Parameter section, you can maintain the following:

Field Value Description
 W Checkbox W stands for Want, you check the parameter you want to be locked
Lock Parameter  Field Name The field name of the table that appears as Lock Parameter name which is locked, you can change it but not recommended
Table Table Name The table that is associated with the field name
Field Field Name The field name of the table that is locked

 

Step 07: Save it, check for Error and activate your Lock Object.

 

Advantages of Lock Objects:

  1. It prevents collision of data in same record.
  2. Lock Object prevents multiple editions of same data at same time.
  3. It tells other programs/user that the table is being currently edited.

*NOTE: There are more topics left in Lock Objects in SAP ABAP like Function Modules of Lock Objects, _SCOPE Parameters, etc that will be discussed in upcoming articles.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »