Mentor SAP
An existing shared lock (“S”) or optimistic lock (“O”) allows other shared locks or optimistic locks for protected display to be set for the same data record. The system rejects the attempts by other programs to set exclusive locks for the same data lock entry.

 

 

Cumulate Locks – Additional Lock Request from the Same Program Instance

Accumulation is the process when an attempt is made to lock a data record more than once while a program is running.

 

The lock system reacts in the following ways during this process:

 

 

Pessimistic Locking Pattern – Setting and Releasing Locks

If you want to ensure that you are reading up-to-date data in your program (with the intention of changing the data and saving it to the database), a pessimistic or an optimistic lock pattern can be applied. The optimistic lock pattern makes use of optimistic locks available as of Application Server (AS) ABAP 7.0 EhP2.

 

To implement the classical (pessimistic) lock pattern, perform the following steps in your program:

  1. Set exclusive locks (“E”) for the data to be processed.
  2. If the lock has been successfully set, read the current data from the database.
  3. Change the program data (user inputs) and update the changes to the database.
  4. Release the locks.

The order of steps ensures that the changes run completely under the protection of locks. Additionally, the order of steps also ensures that only data that the other programs have changed consistently is read.

 

 

Optimistic Locking Pattern – Setting, Promoting, and Releasing Locks

An optimistic lock is set if the user displays data in the change mode. Optimistic locks on the same object cannot collide. If the user wants to save the changed data, the optimistic lock must be converted into an exclusive lock (type “E”).

 

The conversion is implemented using lock mode “R”. The conversion fails if a user has previously set a shared lock on the object, or if the system has already promoted another optimistic lock. If the promotion succeeds, the system deletes the other optimistic locks on the object, and the lock behaves as an exclusive lock.

 

 

Incorrect Usage of Locks

When using locks, if the sequence "LockReadChangeUnlock" is not adhered to, the program will read data from the database currently locked by another program( see the figure for a pessimistic lock pattern). In such a case, even if the lock is successfully set after the read action, the data that the program reads and displays to the user for change is already out of date.