Show TOC

Example Program: SAP LockingLocate this document in the navigation structure

The following example program shows how you can lock and unlock database entries using a lock object.

Tip

REPORT  demo_transaction_enqueue MESSAGE-ID sabapdocu.

TABLES  sflight.

DATA  text(8) TYPE c.

DATA  ok_code TYPE sy-ucomm.

CALL SCREEN 100.

MODULE init OUTPUT.  SET PF-STATUS 'BASIC'.  sflight-carrid = 'LH'. sflight-connid = '400'.ENDMODULE.

MODULE exit INPUT.  LEAVE PROGRAM.ENDMODULE.

MODULE enqueue INPUT.  CASE ok_code.    WHEN 'ENQUEUE'.      CALL FUNCTION 'ENQUEUE_EDEMOFLHT'           EXPORTING                mode_sflight   = 'X'                carrid         = sflight-carrid                connid         = sflight-connid                fldate         = sflight-fldate           EXCEPTIONS                foreign_lock   = 1                system_failure = 2                OTHERS         = 3.      CASE sy-subrc.        WHEN 0.          MESSAGE i888 WITH 'Enqueue successful'(001).        WHEN 1.          text = sy-msgv1.          MESSAGE e888 WITH 'Record already'(002)                             'locked by'(003) text.          CALL TRANSACTION 'SM12'.        WHEN 2 OR 3.          MESSAGE e888 WITH 'Error in enqueue!'(004).      ENDCASE.    WHEN 'DEQUEUE'.      CALL FUNCTION 'DEQUEUE_EDEMOFLHT'           EXPORTING                mode_sflight = 'X'                carrid       = sflight-carrid                connid       = sflight-connid                fldate       = sflight-fldate           EXCEPTIONS                OTHERS       = 1.      CASE sy-subrc.        WHEN 0.          MESSAGE i888 WITH 'Dequeue successful'(005).        WHEN 1.          MESSAGE e888 WITH 'Error in dequeue!'(006).      ENDCASE.    WHEN 'SM12'.      CALL TRANSACTION 'SM12'.  ENDCASE.ENDMODULE.

MODULE select INPUT.  CASE ok_code.    WHEN 'SELECT'.      SELECT * FROM sflight WHERE carrid = sflight-carrid                              AND connid = sflight-connid                              AND fldate = sflight-fldate.      ENDSELECT.      MESSAGE i888 WITH 'SY-SUBRC:' sy-subrc.  ENDCASE.ENDMODULE.

The statically-defined next screen for screen 100 is 100. It uses components of the structure SFLIGHT, copied from the ABAP Dictionary, and looks like this:

The screen flow logic is as follows:

PROCESS BEFORE OUTPUT.  MODULE init.

PROCESS AFTER INPUT.  MODULE exit AT EXIT-COMMAND.  CHAIN.    FIELD: sflight-carrid, sflight-connid, sflight-fldate.    MODULE enqueue.  ENDCHAIN.  MODULE select.

Using the function codes ENQUEUE and DEQUEUE of the GUI status, you can lock and unlock the specified fields of the table SFLIGHT. To do this, use the specifically created lock object EDEMOFLHT via the function modules ENQUEUE_EDEMOFLHT and DEQUEUE_EDEMOFLHT.

In addition, the function code SELECT allows to access the specified fields and the function code SM12 calls the transaction SM12 to list the lock entries in the central locking table.

After a successful lock by a user, no other user can lock the same datasets again. However, every user can access the locked datasets using SELECT if the program does not check the locks in advance with ENQUEUE_EDEMOFLHT.