ABAP - Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Data Consistency → 

SAP Locks

SAP locks must be persisted for the duration of SAP LUWs. For this reason, different work processes and, if applicable, a new application server must be able to handle these locks.

SAP locks are based on lock objects. These are defined in ABAP Dictionary and enable locks to be set on one or more rows in a single database table or locks to be set on rows in multiple database tables associated using foreign key dependencies.

When a lock object is created, two lock function modules are generated whose names consist of the prefixes ENQUEUE_ and DEQUEUE_ and the name of the lock object.

More detailed information can be found under SAP Locking Concept in the SAP NetWeaver documentation in SAP Help Portal.

Setting SAP Locks

The function locks 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 a lock cannot be set because corresponding lock entries already exist in the lock table, the function module terminates with the exception FOREIGN_LOCK.

The most important input parameters of an enqueue function module are:

Parameter Meaning
MODE_dbtab Type of the lock for the database table dbtab of the lock object. Possible values include "S" for a shared lock, "E" for an exclusive lock, "X" for an expanded exclusive lock that can be requested only once (unlike a regular exclusive lock within a program), and "O" for an optimistic lock, which behaves like a shared lock at first, but can be transformed to an exclusive lock.
key_fields For all key fields of the lock object, values can be specified that define the rows to be locked. If no value is specified for a key field, all corresponding rows are locked.
_SCOPE Definition of the lock duration with respect to an SAP LUW. Possible values include "1" for handling the lock in the same program, "2" for passing the lock to the updater, and "3" for handling the lock in the program and in the updater.

Note

An SAP lock is checked by trying to set a corresponding lock and handling the exception FOREIGN_LOCK in the process.

Releasing SAP Locks

SAP locks can be released by deleting the associated entry in the lock table.

When an SAP lock is set using the function module ENQUEUE, the value passed to the input parameter _SCOPE determines the lock duration. Depending on the formal parameter _SCOPE, an SAP lock can be released as follows:

If the function module DEQUEUE is used to release an SAP lock independently of the update, the formal parameter _SCOPE must be given a value that is greater than or equal to the value passed to the same parameter of the function module ENQUEUE.

Besides the _SCOPE parameter, the input parameters of a DEQUEUE function module correspond to those of the ENQUEUE function module. The additional parameter _SYNCHRON can then be used to specify whether the release of the lock should be delayed until program processing is resumed.

Executable Example

SAP Locks, Set and Release



Continue
Example SAP Locks, Set and Release