Example Transaction: SAP Locking 

Übernahme

The following example transaction shows how you can lock and unlock database entries using a lock object. It lets the user request a given flight (on screen 100) and display or update it (on screen 200). If the user chooses Change, the table entry is locked; if he or she chooses Display, it is not.

The example uses the lock object ESFLIGHT and its function modules ENQUEUE_ESFLIGHT and DEQUEUE_ESFLIGHT to lock and unlock the object.

For more information about creating lock objects and the corresponding function modules, refer to the Lock objects section of the ABAP Dictionary documentation.

The PAI processing for screen 100 in this transaction processes the user input and prepares for the requested action (Change or Display). If the user chooses Change, the program locks the relevant database object by calling the corresponding ENQUEUE function.

MODULE USER_COMMAND_0100 INPUT.
  CASE OK_CODE.
    WHEN 'SHOW'....
    WHEN 'CHNG'.
* <...Authority-check and other code...>
      CALL FUNCTION 'ENQUEUE_ESFLIGHT'
EXPORTING
MANDT = SY-MANDT
CARRID = SPFLI-CARRID
CONNID = SPFLI-CONNID
EXCEPTIONS
FOREIGN_LOCK = 1
SYSTEM_FAILURE = 2
OTHERS = 3.
IF SY-SUBRC NE 0.
MESSAGE ID SY-MSGID
TYPE 'E'
NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
     ...

The ENQUEUE function module can trigger the following exceptions:

FOREIGN_LOCK determines whether a conflicting lock already exists. The system variable SY-MSGV1 contains the name of the user that owns the lock.

The SYSTEM_FAILURE exception is triggered if the enqueue server is unable to set the lock for technical reasons.

At the end of a transaction, the locks are released automatically. However, there are exceptions if you have called update routines within the transaction. You can release a lock explicitly by calling the corresponding DEQUEUE module. As the programmer, you must decide for yourself the point at which it makes most sense to release the locks (for example, to make the data available to other transactions).

If you need to use the DEQUEUE function module call several times in a program, it makes good sense to write it in a subroutine, which you then call as required.

The subroutine UNLOCK_FLIGHT calls the DEQUEUE function module for the lock object ESFLIGHT:

FORM UNLOCK_FLIGHT.
     CALL FUNCTION 'DEQUEUE_ESFLIGHT'
          EXPORTING
              MANDT     = SY-MANDT
              CARRID    = SPFLI-CARRID
              CONNID    = SPFLI-CONNID
          EXCEPTIONS
              OTHERS    = 1.
     SET SCREEN 100.
ENDFORM.

You might use this for the BACK and EXIT functions in a PAI module for screen 200 in this example transaction. In the program, the system checks whether the user leaves the screen without having saved his or her changes. If so, the PROMPT_AND_SAVE routine sends a reminder, and gives the user the opportunity to save the changes. The flight can be unlocked by calling the UNLOCK_FLIGHT subroutine.

MODULE USER_COMMAND_0200 INPUT.
  CASE OK_CODE.
    WHEN 'SAVE'....
    WHEN 'EXIT'.
      CLEAR OK_CODE.
      IF OLD_SPFLI NE SPFLI.
         PERFORM PROMPT_AND_SAVE.
      ENDIF.
      PERFORM UNLOCK_FLIGHT.
      LEAVE TO SCREEN 0.
    WHEN 'BACK'....