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.
- 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.
- Once you click the create option, Display Lock Object Screen will appear. Give the short description as a LOCK OBJECT DATA and CTRL+S.
- 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.
- 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.
- The below images will appear on the Repository Info system page for the confirmation of the Lock object 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.
- 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.
- 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.
- 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.
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.
- Once the Test function module screen opens, click Execute icon near the search box or click F8 to execute to unlock the screen.
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.
- Give the table name in which you have created a lock object in the Database table from the ABAP Dictionary screen and click Display.
- Once we click display, the Dictionary: Display Table page will open. Click More –> Utilities –> Table Contents –> Create Entries.
- 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.
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:
- How to Create Table Maintenance Generator in SAP ABAP
- SAP HANA Cloud ABAP Environment
- How to manage roles and privileges for users in SAP HANA Cloud?
- How to Create a Package in SAP ABAP
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.
I am Chris Waldron, working as a Senior SAP HANA Consultant at Halliburton, Houston, Texas, United States. I have been working in SAP for more than 15 years, especially in SAP IT consulting and business consulting. I worked in various industries in Sales & Distribution, Customer Relationship Management, banking, Risk Management, etc. And I am an SAP Certified Development Specialist – ABAP for SAP HANA 2.0 and SAP HANA Modeling Certified consultant. Read more