I call some RFC module externally which calls other modules and calculates some value inside the call stack and saves it to memory, then reads this value in the end and returns to external caller. The calculation can be different each time so we need to read the memory area accordingly.
The complications of the current case are:
The idea is to write to memory with the ID only unique for the current call, not the previous ones, i.e. the value in the memory will always be relevant.
Let me illustrate the case with this simple FM I created. The module will return the value only if it is set in the current call, i.e. only relevant one.
FUNCTION Z_MEM.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" EXPORTING
*" VALUE(E_PAR) TYPE CHAR50
*"----------------------------------------------------------------------
DATA id LIKE sy-timlo.
IMPORT id FROM MEMORY ID 'MAN'.
IF id IS INITIAL.
id = sy-timlo.
EXPORT id TO MEMORY ID 'MAN'.
e_par = id.
ENDIF.
ENDFUNCTION.
I tested it from the outside in a loop for 20 times and got:
>>> 154251
...
>>>
So, the memory is reused as expected and all the further runs may consume the wrong value.
Possible solutions I see here:
Is there a standard way to uniquely identify RFC call? Maybe some SY
field?
P.S. In this comment and here they say (and it is quite obvious) closing connection after each call will be slower than usual, so it is not desirable.
The ideas from the comments were useful but unachievable in my case.
Yes, I could generate the GUID, but the problem was in the way of reading/writing the memory to the same ID. The include module that writes the value to memory was located deeply in the call stack, and the READ was conducted in the RFC FM body, so to read/write the value from same memory ID I should have been passed the GUID generated at the RFC entry point (root level of call stack) to the 10th nesting level of call stack, which is obviously impossible, or at least I didn't find a way how to do it.
This is how I solved the problem. I found an FM which does exactly what I need
CALL FUNCTION 'TH_GET_SESSION_ID'
IMPORTING
session_id = id.
I ran some tests with external calling and can confirm that ID generated by the FM is persistent throughout the whole RFC call, but different between calls.