How to Create Lock Object in SAP ABAP?

In this SAP tutorial, I will discuss, What is a lock object in SAP ABAP, and How to Create Lock Object in SAP ABAP. Also, I will tell you what are the advantages of lock objects.

Introduction to Lock object in SAP ABAP

In SAP, there are multiple users can access the same data from a database simultaneously. To restrict this, SAP provides a feature called a lock object. In SAP, lock objects are used to prevent inconsistent or duplicate data when it is added to or updated in the database.

If you are modifying or updating data in a database table in SAP, we need to ensure that other users will not give the same repeated data. So, to make sure of this, we need to define a lock object for the tables. Once you define the lock object, it will not allow other users to give previous data.

Also, Read: How to Create Table in SAP ABAP [With Columns]

How to Create Lock Object in SAP ABAP

Now, we will see, how to create a lock object in SE11 TCODE from SAP ABAP.

  • Open SAP ABAP and then enter the transaction code SE11 in the search box.
Create Lock object in SAP ABAP
  • Then the ABAP dictionary screen will open. It is mandatory to give a name starting with E. Here select the Lock object and then enter a name starting with E.
  • Here I have given the lock object name EZ_LOCKDATA. Then click on Create button.
Lock object creation in SAP ABAP
  • Once you click the create option, Display Lock Object Screen will appear. Give the short description as a LOCK OBJECT DATA and CTRL+S.
Create Lock Object in SAP ABAP
  • On the same page, select the Tables column and choose the table you want to lock, and give the Lock Mode as Write Lock from the list of options such as shared lock, exclusive lock, and optimistic lock.
    • Shared Lock – Many users can try to access locked data at the same time in display mode.
    • Exclusive Lock – It protects the locked object against all types of locks from other transactions. The same person is only able to reset the lock.
    • Exclusive, not cumulative – Exclusive, non-cumulative locks can only be requested once by the same person. If the request is received again then it will be rejected.
    • Optimistic Lock – Optimistic locks also work similarly to shared locks at first then they will behave like an exclusive lock later.
Create Lock object in SAP ABAP
  • Save the Lock object by Ctrl+S and save it in a local object then activate the lock object by clicking CTRL+F3 in SAP ABAP.

This is how we should create the Lock object in SAP ABAP. The same person is only able to reset the lock.

Check out: Operators in SAP HANA Cloud

How to View the Lock Object in SAP ABAP

  • To see if the data or object are locked, go to More–>Go to–>Lock Modules in the Display Lock Object page.
Lock Modules in Lock Object Data
  • The below images will appear on the Repository Info system page for the confirmation of the Lock object in SAP ABAP.
Object Locked in SAP ABAP

By the above method, we can see the lock module in the SE11 TCODE from SAP ABAP.

Check Lock Object in SE37 Transaction Code(ENQUEUE)

  • Once we have created a lock object in SE11, go to the SAP Easy Access page, type the SE37 Tcode in the search bar, and hit enter to execute the lock object.
Lock object in SE37 SAP ABAP
  • Here Function Builder: Initial Screen will open. Give the Function Module name as ENQUEUE_EZ_LOCKDATA and click the Test/Execute icon at the top or click F8.
Local Object in SE37 TCODE
  • The Function Module name is ENQUEUE_EZ_LOCKDATA which we got from the lock module screen in the SE11 Tcode. Copy that Enqueue field and paste it here in the Function Module field. Check the below screenshot for clarity.
ENQUEUE Lock Object in AP ABAP
  • In this step, the Test function Module: Initial Screen will be displayed. Click on the Execute button or F8 to execute, and hit CTRL+S. The particular table will be locked.
Data lock in SE37 TCODE

This is the method we should use to create a Lock object in the SE37 transaction code from the SAP ABAP.

Have a look: How to Install ABAP Development Tools

How to Unlock Object in SAP ABAP

We will see how to unlock the data or table in the SAP ABAP from SE37 TCODE.

  • Give the Function Module name as DEQUEUE_EZ_LOCKDATA and click the Test/Execute icon at the top or click F8.
DEQUEUE Table in se37 Tcode
  • Once the Test function module screen opens, click Execute icon near the search box or click F8 to execute to unlock the screen.
Execute Lock Object in SE37 TCODE

By the above method, we can Dequeue the locked table in the SAP ABAP.

What happens when we try to Add Existing Entries in Table

Now, let us see, what will happen when we will try to add an existing entry to the table for which we have added the lock object.

Go to your table which you have made a lock object in SE11. Follow the below steps to check the lock object from that table.

  • Open SAP ABAP and give the transaction code SE11 in the search box to check the table.
Open SAP ABAP for Transaction Code
  • Give the table name in which you have created a lock object in the Database table from the ABAP Dictionary screen and click Display.
View se11 for table in SAP ABAP
  • Once we click display, the Dictionary: Display Table page will open. Click More –> Utilities –> Table Contents –> Create Entries.
Lock object in SAP ABAP
  • In the Output, give the entries which exist in the table and save or click CTRL+S. The message will be pop-up like A data record with the specified key field already exists. This shows we can not create duplicate entries in the table once a lock object is created.
  • This prevents the table from containing repeated data.
Output for Lock Object in SAP ABAP

Advantages of Lock Object in SAP ABAP

  • They protect the data among more users and also save the integrity and uniformity of data across objects.
  • Data congestion will be reduced, if someone tries to see the data.
  • Secured data is possible in the Lock object.
  • The Lock Objects notify another user that a shared object is currently being used by one particular user.
  • It doesn’t allow us to enter important and unique details like phone numbers so that confidential data can be maintained.

Furthermore, you may like some more SAP tutorials:

Conclusion

From this tutorial, we have learned what are lock objects and their introduction, how to create a lock object, and its benefits in SAP ABAP.