2010 Jan 13 9:40 PM
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
2010 Jan 13 10:07 PM
2010 Jan 14 12:10 AM
2013 Dec 11 6:52 AM
-----> 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
2022 Jun 02 7:56 AM
in this case, if there is a program dump or error will the lock release itself ?
2022 Jun 02 4:27 PM
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.