Shared memory-Sharing Objects across Programs
SHARED MEMORY
As you already know there is often cases that we need to share common data across programs.
This is a result of business requirements for the purpose of this blog i like to call it as Buffer.
The meaning i use here is not the shared buffer concept in abap which is also another topic.
So we need to store in a place like buffer and share the memory.
As an example: In the latest project i was working on there was a requirement where i needed to store all the data that user has entered inside a Goods Movement
Till the user goes and saves sit i should be able to store the data in the Z interface that i developed.
So i needed to store the data which was a complex one.
Export to MEMORY ID concept is the most primitive one but not very powerful when you are needing to store smth very complex like an object
So I had to unleash the Shared memory and learn it to apply in the project.
Lets start applying here !!!
STEPS TO CREATE SHARED MEMORY CONCEPT IN ABAP
In the example below i like to give you a shared memory where you can store an object or data.
Step 1– Create the Shared memory Object- What will you store?
Create the object called zcl_shared_root-DO NOT FORGET TO MARK IT AS SHARED MEMORY ENABLED
ZCL_SHARED_ROOT |
---|
class ZCL_SHARED_ROOT definition public final create public shared memory enabled . public section. interfaces IF_SHM_BUILD_INSTANCE . data OREF type ref to OBJECT . data DREF type ref to DATA . methods SET_OREF importing !IO_OREF type ref to OBJECT . methods GET_OREF returning value(RO_OREF) type ref to OBJECT . methods SET_DATA importing !IO_DREF type ref to DATA . methods GET_DATA returning value(RO_DREF) type ref to DATA . protected section. private section. ENDCLASS. CLASS ZCL_SHARED_ROOT IMPLEMENTATION. method GET_DATA. ro_dref = dref. endmethod. method GET_OREF. ro_oref = oref. endmethod. method SET_DATA. dref = io_dref. endmethod. method SET_OREF. oref = io_oref. endmethod. ENDCLASS. |
Attributes: oref as reference to another object
dref as a reference to any data
Step 2– Create the Shared memory Area- The memory area that wıill store your object
Check SHMA is quite useful !!
Question: Automatic Preload is a good feature, when we first read the data it will throw a dump so to avoid this we need to use automatic preload feature!!
Here the name of the Area is zcl_shared_areaç notice the root class name is our root at above and auto are creation is ticked !!
This is because of the interface we implemented at the zcl_shared_root object
if_shm_build_instance
Step 3 – Create a the Object we want to Save called zcl_object_saved
Note that its shared memory enabled as well!!
Example data taken from sflight database!!
zcl_object_saved |
---|
class zcl_object_saved definition public final create public shared memory enabled . public section. data flight_list type sflight_tab1 . methods add_flight importing !is_flight type sflight . methods get_flight_list returning value(rt_flight_list) type sflight_tab1 . protected section. private section. endclass. class zcl_object_saved implementation. * <SIGNATURE>—————————————————————————————+ * | Instance Public Method ZCL_OBJECT_SAVED->ADD_FLIGHT * +————————————————————————————————-+ * | [—>] IS_FLIGHT TYPE SFLIGHT * +————————————————————————————–</SIGNATURE> method add_flight. read table flight_list transporting no fields with key carrid = is_flight–carrid connid = is_flight–connid fldate = is_flight–fldate . if sy–subrc ne 0. append initial line to flight_list reference into data(lr_flight). lr_flight->* = is_flight. endif. endmethod. * <SIGNATURE>—————————————————————————————+ * | Instance Public Method ZCL_OBJECT_SAVED->GET_FLIGHT_LIST * +————————————————————————————————-+ * | [<-()] RT_FLIGHT_LIST TYPE SFLIGHT_TAB1 * +————————————————————————————–</SIGNATURE> method get_flight_list. rt_flight_list = flight_list. |
Step 4 – Implement the Build of the Automatic preload!! interfaces if_shm_build_instance
Implement the code inside ZCL_SHARED_ROOT build!!
ZCL_SHARED_ROOT |
---|
method if_shm_build_instance~build. data: lo_handle type ref to zcl_shared_area, lo_root type ref to zcl_shared_root, lo_object_saved type ref to zcl_object_saved, ls_sflight type sflight . *Autamatic PreLoad!! try. lo_handle = zcl_shared_area=>attach_for_write( ). create object lo_root area handle lo_handle. lo_handle->set_root( lo_root ). create object lo_root->oref area handle lo_handle type zcl_object_saved. lo_object_saved ?= lo_root->oref. **get data select single * from sflight into ls_sflight. **Set a data lo_object_saved->add_flight( is_flight = ls_sflight ). lo_handle->detach_commit( ). catch cx_shm_error into data(lo_exception). raise exception type cx_shm_build_failed exporting previous = lo_exception. endtry. if invocation_mode = cl_shm_area=>invocation_mode_auto_build. call function ‘DB_COMMIT’. endif. endmethod. |
Step 5 -Create a test program!!
ZTEST_SHARED |
---|
*&———————————————————————* *& Report ZTEST_SHARED *& *&———————————————————————* *& *& *&———————————————————————* report ztest_shared. class lcl_test definition. public section. class-methods: test_auto_build. class-methods: test_write. endclass. class lcl_test implementation. method test_auto_build. data: lo_handle type ref to zcl_shared_area, lo_root type ref to zcl_shared_root, lo_object_saved type ref to zcl_object_saved, ls_sflight type sflight . *Autamatic PreLoad!! try. lo_handle = zcl_shared_area=>attach_for_read( ). lo_object_saved ?= lo_handle->root->oref. lo_handle->detach( ). catch cx_shm_attach_error into data(lo_exc). endtry. endmethod. method test_write. data: lo_handle type ref to zcl_shared_area, lo_root type ref to zcl_shared_root, lo_object_saved type ref to zcl_object_saved, ls_sflight type sflight . try. lo_handle = zcl_shared_area=>attach_for_write( ). create object lo_root area handle lo_handle. lo_handle->set_root( lo_root ). create object lo_root->oref area handle lo_handle type zcl_object_saved. lo_object_saved ?= lo_root->oref. **get data select single * from sflight into ls_sflight. **Set a data lo_object_saved->add_flight( is_flight = ls_sflight ). lo_handle->detach_commit( ). catch cx_shm_error into data(lo_exception). raise exception type cx_shm_build_failed exporting previous = lo_exception. endtry. endmethod. endclass. start-of-selection. break solend. call method lcl_test=>test_auto_build. |
Step 6- Observe the memory Area Created!!
Go to shma and choose the memory area-zcl_shared_area
As you can see you can check the memory allocated for the Shared memory and inside our memory we managed to store another object!!
This could be a much complex object!!
Conclusion: So i can read this memory area and access the object from another program which is great is not it.
I hope you find my blog useful!!
Thanks all
❗ Just a word of caution as with other memories (except the INDX-type cluster tables) SHM is application server specific.
So if you have a load-balanced architecture where multiple appl servers are involved, SHM might get tricky.
BR,
Suhas
Hi,
Thanks for the article .
Question - Is that a session based ? (each user have its own copy) .
Regards.
Hello Eitan,
AFAIK it isn't. May be diagram here clarifies the scope of each memory type - ABAP Keyword Documentation.
BR,
Suhas
Hi,
Many thanks.
So we can use it for a chatting program 😉 ?
What do we have for "user session" level ? can the technic described in this article can be used in "SAP Memory" or "ABAP Memory" .
Regards.
This is adjustable in transaction SHMA.
Take a look on Area "CL_WDR_TEST_CHAT_SHM_AREA". It is used on an chat demo application done in web dynpro.
As it's possible to make a chat among different users, it is not always session based.
Hi,
So what can replace "Export to MEMORY ID" which I do not like 😥 ?
You mention "This is adjustable in transaction SHMA" elaborate please.
Regards.
You can maintain the visibility/scope of the instance of an SHM-enabled class via SHMA (trxn).
Tbh, i was not aware of this feature (may be i forgot 😐 ). I have never used an SHM class in productive code since i think they are an over-kill.
BR,
Suhas
Hi,
IMHO the "where used" worth it.
Regards.
Hi Solen,
Once shared memory area is created we can access it anywhere after that we can free memory area that is used by us?
Regards,
Ravi Shankar.L
Hi guys
I apologize i just saw your comments
I didnt login for a while:)
Shared memory is quite interesting idea as its an oo approach
Ravi this shared memory concept is best to use when the data you use doesnt change very often
When you load the data into the shared memory that will stay there till you delete the data
You can update or add new data as well
Via SHMM you can monitor the data and see how much memory it utilized
Yes it will definetely replace Export statement !!
THere are quite good demos to look at
check table SHMA_ATTRIBUTES
and see the attribute life_context
which describes the lifetime of a shared memory
It could be server level or session based or abap memory !!