Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing External Data →  Data Consistency →  SAP Locks 

Locking and Unlocking

This example demonstrates the setting up and releasing of SAP locks.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT  demo_transaction_enqueue MESSAGE-ID sabapdemos.

TABLES  demo_conn.

DATA sflight_tab TYPE TABLE OF sflight.

DATA  text TYPE c LENGTH 8.

DATA  ok_code TYPE sy-ucomm.

CALL SCREEN 100.

MODULE init OUTPUT.
  SET PF-STATUS 'BASIC'.
  demo_conn-carrid = 'LH'. demo_conn-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         = demo_conn-carrid
          connid         = demo_conn-connid
          fldate         = demo_conn-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)
                            'SY-SUBRC:' sy-subrc.
      ENDCASE.
    WHEN 'DEQUEUE'.
      CALL FUNCTION 'DEQUEUE_EDEMOFLHT'
        EXPORTING
          mode_sflight = 'X'
          carrid       = demo_conn-carrid
          connid       = demo_conn-connid
          fldate       = demo_conn-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
             INTO TABLE sflight_tab
             WHERE carrid = demo_conn-carrid
                              AND connid = demo_conn-connid
                              AND fldate = demo_conn-fldate.
      MESSAGE i888 WITH 'SY-SUBRC:' sy-subrc.
  ENDCASE.
ENDMODULE.

Description

Using the GUI status function codes ENQUEUE and DEQUEUE, the specified fields in the table SFLIGHT can be locked and unlocked. To do this, the lock object EDEMOFLHT is used in conjunction with the relevant function modules ENQUEUE_EDEMOFLHT and DEQUEUE_EDEMOFLHT .

Using the function code SELECT, access to the specified fields is still possible. The function code SM12 calls the transaction SM12 to display the lock entry in the central lock table.

After a user has successfully locked a data record, the same data record cannot be locked again by a different user. However, all users can access the locked data records using the Open SQL statement, assuming the program has not already checked the locks using ENQUEUE_EDEMOFLHT.