Entering content frame

The SAP Lock Concept Locate the document in its SAP Library structure

Reasons for Setting Locks

Suppose a travel agent want to book a flight. The customer wants to fly to a particular city with a certain airline on a certain day. The booking must only be possible if there are still free places on the flight. To avoid the possibility of overbooking, the database entry corresponding to the flight must be locked against access from other transactions. This ensures that one user can find out the number of free places, make the booking, and change the number of free places without the data being changed in the meantime by another transaction.

Lock Mechanisms in the Database System

The database system automatically sets database locks when it receives change statements (INSERT, UPDATE, MODIFY, DELETE) from a program. Database locks are physical locks on the database entries affected by these statements. You can only set a lock for an existing database entry, since the lock mechanism uses a lock flag in the entry. These flags are automatically deleted in each database commit. Database locks are therefore never available longer than for only one database LUW. That means that in ABAP application programming, database locks can no longer exist than the duration of one dialog step.

The physical lock mechanisms in the database system are therefore insufficient for the requirements of an ABAP transaction. The locks must remain set for the duration of a whole SAP LUW, that is, over several dialog steps. They must also be capable of being handled by different work processes and even different application servers. Consequently, each lock must not only apply on the application server that carries out the locking transaction, but on all installed servers of the SAP Web AS ABAP.

SAP Locks

To complement the SAP LUW concept, in which bundled database changes are made in a single database LUW, the SAP System also contains a lock mechanism, fully independent of database locks, that allows you to set a lock that spans several dialog steps. These locks are known as SAP locks.

The complete documentation on SAP locks can be found in Structure linkThe SAP Lock Concept. This is a short introduction within the environment of ABAP programming.

The SAP lock concept is based on lock objects. Lock objects allow you to set SAP locks for entire application objects. An application object consists of one or more entries in a database table, or entries from more than one database table that are linked using foreign key relationships.

Before you can set an SAP lock in an ABAP program, you must first create a lock object in the ABAP Dictionary. A lock object definition contains the database tables and their key fields on the basis of which you want to set a lock. When you create a lock object, the system automatically generates two function modules with the names ENQUEUE_Name and DEQUEUE_Name. You can then set and release SAP locks in your ABAP program by calling these function modules in a CALL FUNCTION statement.

This graphic is explained in the accompanying text

See also: Example Transaction: SAP Locking

These function modules are executed in a special enqueue work process. Within an SAP System, enqueue work processes run on a single application server. This server maintains a central lock table for the entire SAP System in its shared memory.

The enqueue function module sets an SAP lock by writing entries in the central lock table. If the lock cannot be set because the application object (or a part of it) is already locked, the function module informs the user. The following figure shows the components involved in the lock process:


This graphic is explained in the accompanying text


Unlike the database, which sets physical locks, the SAP lock mechanism sets logical locks. This means that:

        A locked database entry is not physically locked in the database table.

The lock entry is merely entered as a lock argument in the central lock table. The lock argument is made up of the primary key field values for the tables in the lock object. These are import parameters of the enqueue function module. The lock is independent of database LUWs. It is released either implicitly when the database update or the SAP transaction ends, or explicitly, using the corresponding dequeue function module. You can use a special parameter in the update function module to set the exact point at which the lock is released during the database update.

        A locked entry does not necessarily have to exist in a database table.

You can, for example, set a lock as a precaution for a database entry that is not written to the database until the update at the end of the SAP LUW.

        The effectiveness of the locks depends on cooperative application programming.

Since there are no physical locks in the database tables themselves, all programs that use the same application objects must look in the central table themselves for any locks. There is no mechanism that automatically prevents a program from ignoring the locks in the lock table.

Lock Types

There are four types of locks in the SAP System:

        Shared lock

        Exclusive lock

        Exclusive but not cumulative lock

        Optimistic lock

        Structure link→ see also

Lock Duration

When you set a lock, you should bear in mind that if it remains set for a long time, the availability of the object to other transactions is reduced. Whether or not this is acceptable depends on the nature of the task your program is performing.

Remember in particular that setting too many shared locks without good reason can have a considerable effect on programs that work with database tables. If several programs running concurrently all set a shared lock for the same application object in the SAP system, it can make it almost impossible to set an exclusive lock, since the program that needs to set that lock will be unable to find any time when there are no locks at all set for that object. Conversely, a single exclusive lock prevents all other programs from reading the locked object.

At the end of an SAP LUW, you should release all locks. This either happens automatically during the database update, or explicitly, when you call the corresponding dequeue function module. Locks that are not linked to a database update are released at the end of the SAP transaction.



Leaving content frame