Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to check if ABAP program is running in another instance?

Former Member
0 Kudos
3,329

Hey Guys,

I need to ensure that a given ABAP program is only running in one instance.

Here is what I tried so far:

1) FM TH_WPINFO

The problem with this is that the Z (custom) program calls lot of SAP function modules and when inside those, the WP_REPORT field of WPLIST table has something else and not the Z program name.

2) FM ENQUEUE_ESINDX

When I used the code below the following happened:

-The First instance runs fine

-The Second instance fails in the locking and exits the way it is supposed to but then

-If I run the program the Third time, it runs with a succesful lock - probably because the after second instance the lock was cleared???

CALL FUNCTION 'ENQUEUE_ESINDX'
    EXPORTING
     MODE_INDX            = 'E'
     MANDT                = SY-MANDT
     RELID                = 'ZZ'
     SRTFD                = PROGRAM
*     SRTF2                =
*     X_RELID              = ' '
*     X_SRTFD              = ' '
*     X_SRTF2              = ' '
*     _SCOPE               = '2'
*     _WAIT                = ' '
*     _COLLECT             = ' '
    EXCEPTIONS
      FOREIGN_LOCK         = 1
      SYSTEM_FAILURE       = 2
      OTHERS               = 3.

Any other idea, how I could accomplish this?

Thanks a lot,

Viktor

5 REPLIES 5

former_member156446
Active Contributor
0 Kudos
655

Check this discussion:

Former Member
0 Kudos
655

You are going good. check this thread:

[]

Former Member
0 Kudos
655

-----> Include this perform in Initalization or in Start-of-Selection screen event.

*&---------------------------------------------------------------------*

*&      Form  LOCK_CURRENT_INSTANCE

*&---------------------------------------------------------------------*

* Perform to lock the current instance of the

* program, so that only one

* instance can be runned at a given time.

*----------------------------------------------------------------------*

FORM lock_current_instance .

 

*---FM to lock the current instance of the program

    CALL FUNCTION 'ENQUEUE_E_TRDIR'

      EXPORTING

        mode_trdir     = abap_true

        name           = sy-repid

      EXCEPTIONS

        foreign_lock   = 1

        system_failure = 2

        OTHERS         = 3.

    IF sy-subrc <> 0.

      IF sy-batch = abap_true.

        MESSAGE e016 WITH 'Already one Instance of the Program is Running'.

      ELSE.

        MESSAGE s016 WITH 'Already one Instance of the Program is Running' DISPLAY LIKE 'E' .

        LEAVE LIST-PROCESSING.

      ENDIF.

    ENDIF.

ENDFORM.                    " LOCK_CURRENT_INSTANCE

---->and include this perform as a last perform in End-of-Selection screen event.

*&---------------------------------------------------------------------*

*&      Form  UNLOCK_CURRENT_INSTANCE

*&---------------------------------------------------------------------*

* This perform is used to unlock the instance of the

* running program

*&---------------------------------------------------------------------*

FORM unlock_current_instance .

*---FM to release the lock on the running program

  CALL FUNCTION 'DEQUEUE_E_TRDIR'

    EXPORTING

      mode_trdir = abap_true

      name       = sy-repid.

ENDFORM.                    " UNLOCK_CURRENT_INSTANCE

0 Kudos
655

in this case, if there is a program dump or error will the lock release itself ?

0 Kudos
655

Maybe create/update a Z table with a flag? Set it to X if it's initial, exit if already X, and clear when program ends.

You'd have to handle the situation if the program crashes before clearing the flag though.