2020 Aug 22 8:21 AM
Hi All,
I am looking to make a ABAP class "A" singleton in ABAP.
I can not just rely on the usual singleton pattern implementation in ABAP memory because each HTTP requests incoming to ABAP have a new session. So the singleton with plain memory will not work.
I explored shared memory and then found that we can put object instances in shared memory.
I am getting the following exception CX_SHM_EXTERNAL_REFERENCE
As per https://answers.sap.com/questions/6058360/shared-objects-cxshmexternalreference-exception.html and https://answers.sap.com/questions/12905922/sharing-runtime-objects-to-shared-memory-using-shm.html it seems this is not possible to keep objects in memory.
Best Regards,
Saurav
2020 Aug 22 10:24 AM
Is your goal to have a stateful HTTP session? Why can't you reinstantiate the object? Modern usage is to have a stateless/REST one. Maybe you should explain the whole context to get more precise help. Moreover, be careful that you may also have several application servers, i.e. different memories...
2020 Aug 22 10:53 AM
Hi Sandra,
Thanks for the reply.
I have a fiori application which connects to ABAP. backend which connects to an external service through OAuth mechanism.
Constructor of the class does some initialisation for connection to this external service like communicate using OAuth.
I don't want all the subsequent requests to go through this initialisation code each and every time.
Hence wanted to make this class singleton across the HTTP requests coming from the Fiori application
Best Regards,
Saurav
2020 Aug 22 3:33 PM
I think this is possible. Found some pointers here
https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-us/abencreate_shared_object_abexa.htm
As per sample program i will point the root-oref to my class type.
At least the report program is working. I need to test it with the actual HTTP requests coming into ABAP system.
2020 Sep 06 8:25 PM
Is the external services called inside of a code-based OData service? If it is, then soft state should be a better solution then persistence objects.