Skip to Content
Technical Articles

Code Snippet Series: Wait a Fraction of a Second

This post is part of a series on code snippets. The complete list of posts in the series is available in the document Code Snippets: A Blog Series.

I occasionally need to wait for a lock entry to be dequeued when I have a multi-step scenario where COMMIT WORK AND WAIT is insufficient but waiting a static number of seconds with WAIT UP TO n SECONDS could result in waiting longer than necessary.

With the below code, I reduce wait time to the shortest possible wait–depending on use, even down to one tenth of a second.

DATA lt_enq TYPE STANDARD TABLE OF seqg3.

DATA: BEGIN OF ls_time,
        start   TYPE timestampl,
        now     TYPE timestampl,
        elapsed TYPE tzntstmpl, " in seconds
        limit   TYPE tzntstmpl VALUE 3, " in seconds
      END OF ls_time.

GET TIME STAMP FIELD ls_time-start.

WHILE ls_time-elapsed < ls_time-limit.

  CALL FUNCTION 'ENQUEUE_READ'
    EXPORTING
      gname                 = TABLE_NAME
      garg                  = TABLE_KEY
    TABLES
      enq                   = lt_enq
    EXCEPTIONS
      communication_failure = 1
      system_failure        = 2
      OTHERS                = 3.

  IF lt_enq[] IS INITIAL.
    EXIT. " object is not locked
  ENDIF.

  GET TIME STAMP FIELD ls_time-now.

  ls_time-elapsed = cl_abap_tstmp=>subtract(
    tstmp1 = ls_time-now
    tstmp2 = ls_time-start
  ).

ENDWHILE. " elapsed time
23 Comments
You must be Logged on to comment or reply to a post.