Application Development and Automation 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: 

Singleton in ABAP

former_member210955
Active Participant
0 Kudos
825

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

4 REPLIES 4

Sandra_Rossi
Active Contributor
0 Kudos
482

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...

former_member210955
Active Participant
482

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

former_member210955
Active Participant
0 Kudos
482

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.

Former Member
0 Kudos
482

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.