ABAP Keyword Documentation → ABAP - Reference → Processing External Data → Data Consistency
SAP Locks
SAP locks must be maintained for the duration of SAP LUWs. For this reason, various work processes and, if applicable, changing application servers must be able to handle these locks.
SAP locks are based on lock objects. The ABAP Dictionary defines these objects; they permit locks of single or several lines in one database table or locks of lines in several database tables linked by foreign key dependencies.
The creation of a lock object generates two lock function modules, whose names consist of the prefixes ENQUEUE_ and DEQUEUE_ and the name of the lock object.
See also SAP Locking Concept.
Other versions: 7.31 | 7.40 | 7.54
Setting SAP Locks
Lock function modules set or release SAP locks when called. The function modules access a central lock table in the memory of a special application server work process. Every AS ABAP contains exactly one such table (administered by transaction SM12). An enqueue function module sets an SAP lock by writing a corresponding entry in the lock table. If you cannot set a lock because corresponding lock entries already exist in the lock table, the function module terminates with the FOREIGN_LOCK exception.
The most important entry parameters of an enqueue function module are:
Parameter | Meaning |
---|---|
MODE_dbtab | Type of lock for the dbtab database table of the lock object. Possible entry values include "S" for ashared lock, "E" for anexclusive lock, "X"for an expanded exclusive lock that can be requested only once unlike a regular exclusive lock withina program, and "O" for an optimistic lock which behaves like a shared lock at first, but can be converted to an exclusive lock. |
key_fields | For all key fields of the lock object, you can specify values that define the rows to be locked. If you have not specified a value for a key field, all corresponding rows are locked. |
_SCOPE | Definition of the lock duration with respect to an SAP LUW. Possible entry values include "1" for handling the lock in the same program, "2" for transferring the lock to theupdate, and "3" for handling the lock in the program and in the update. |
Note
You can check an SAP lock by trying to set a corresponding lock and handling the FOREIGN_LOCK exception in the process.
Releasing SAP Locks
You can release SAP locks by deleting the corresponding entry in the lock table.
When you set an SAP lock using the ENQUEUE function module, the value transferred to the _SCOPE entry parameter determines the lock duration. Depending on the formal parameter _SCOPE, you can release an SAP lock as follows:
- If _SCOPE is set to 1, the lock is not linked to the current SAP LUW. You can release the lock either by calling the DEQUEUE function module if the formal parameter _SCOPE is passed the value "1" or "3" , or by terminating the program.
- If _SCOPE is set to 2, the lock is linked to the current SAP LUW. In case
CALL FUNCTION ... FOR UPDATE TASK
has registered at least one update function module, the statementCOMMIT WORK
orROLLBACK WORK
releases the lock upon completion of the SAP LUW. When usingCOMMIT WORK
, the update function releases the lock after processing the update function modules. This type of lock may persist beyond the end of a program until the update procedure has been completed.
- If _SCOPE is set to 3, both the update function and the program must release the lock. The update function releases the lock as if _SCOPE had a value of 2. The program releases the lock as if _SCOPE had a value of 1. The entire release procedure is specified by the last user who released the lock.
If you want to release an SAP lock using the DEQUEUE function module independent of the update function, you must transfer a value to the formal parameter _SCOPE that is greater than or equal to the value transferred to the parameter of the same name for the ENQUEUE function module.
Besides the _SCOPE parameter, the entry parameters of a DEQUEUE function module correspond to those of the ENQUEUE function module. You can then use the additional parameter _SYNCHRON to specify whether the release of the lock should be delayed until the program processing continues.