You use a logical lock mechanism to synchronize access by several programs to the same data. This lock mechanism fulfills two main functions:
The data records of a table to be locked are defined by a logical condition. When a lock is set, the system enters this logical condition in a lock table . This entry is retained until it is removed by the program or the program comes to an end. The system removes all the locks set by a program at the end of the program. In the following figure you can see the mechanism for locking a table.
When accessing data records, the records just being edited by other programs can be identified by the entry in the lock table. Such an entry for the lock must define a number of fully specified key fields, that is, either a value is passed for the key field or the lock for this field is generic.
Setting a lock
To set locks, a lock object must be defined in the ABAP Dictionary. When you activate this lock object, two function modules (see Function Modules for Lock Requests ) are generated with the names ENQUEUE_<lockobjectname> and DEQUEUE_<lockobjectname>.
If data records are to be locked, you must call function module ENQUEUE_<lockobjectname>. The values of the key fields that specify the records to be locked are passed for all the tables contained in the lock object when the function module is called. There is a generic lock if a value is not passed for all the key fields. The function module writes the appropriate lock entry. For more information, see Example of Lock Objects . If another program also requests a lock, it is accepted or rejected depending on the lock mode . For more information, see Lock Collisions . The program can then react to this situation.
Locked data records can be unlocked by calling function module DEQUEUE_<lockobjectname>. The key values and the lock mode used to set the lock must be passed to the function module. The following figure shows what happens when a program calls the ENQUEUE function module of the lock object.
This lock procedure requires all programs that are used to cooperate with each other. Inconsistencies can occur if a program reads or changes data without having previously locked it. When you set a lock, the data records are protected only against changes by another program if this program also requests a lock before accessing the data.
Instead of writing lock requests or lock releases directly in the lock table, it is also possible to collect them first in a local lock container . The collected locks can be sent at a later time as a group. A parameter of the relevant function module controls whether the system sends the lock requests or lock releases directly.
For more information about the lock concept and how lock management works, see The SAP Lock Concept .