0046 - Using Shared Objects

Moderators: Snowy, thx4allthefish, Rich, ilya

Post Reply
Posts: 7116
Joined: Thu Oct 31, 2002 4:47 pm
Location: Liverpool

0046 - Using Shared Objects

Post by Rich » Sat Mar 28, 2009 8:51 am

Using Shared Objects.

Shared objects are SAP's answer to an age-old request by programmers. Up until now, the only way to share variables between programs was to use EXPORT/IMPORT TO/FROM MEMORY. This method was very slow because the "sending" program had to copy variables from its memory to the shared memory, and the "receiving" program had to copy these variables from the shared memory into its own program memory. This generated two copies for basically nothing.
Here are the benefits of shared objects:
  • Copy-free: very fast access
  • Version management: you can handle many version of the variables at the same time
  • Preloading: you can now request a specific variable no matter if the "sending" program already created them
  • Automatic propagation from a database or to different application servers
  • Could be client dependant
  • Easy monitoring
Here is a very simple example but you can do very powerful shared object, see documentation for more information.

Step 1 :

You have to define a Root class using SE24, In this class you will defined all variables you want to share in the attributes tab, This class must be "Shared memory enabled" :


Step 2 : You have to define your shared memory object using the transaction SHMA. It is important to put the class you created in step 1 in the field "root class".


Step 3 : You have to write a program to write to the shared memory.

Code: Select all

DATA : hdl  TYPE REF TO zcl_sm_area,
       root TYPE REF TO zcl_root_area.
* Create the default instance for write, 
* when we attach for write, an exclusive lock is performed on the shared memory object  
  hdl = zcl_sm_area=>attach_for_write( ).

*when we create our root object to access variables, we must use the new keyword addition AREA HANDLE
* Create the link between the root object and the shared memory object
  hdl->set_root( root ).
* put data in the shared memory variable
  root->big_var = 'hello world!'.
* commit
hdl->detach_commit( ).  
Step 4 : You have to create a program to read from the shared memory

DATA : hdl TYPE REF TO zcl_sm_area.
* Open default instance for read
  hdl = zcl_sm_area=>attach_for_read( ).
*Access root object component to write the variable
  WRITE / hdl->root->big_var.
*Release lock
  hdl->detach( ).
Step 5: You can monitor the result in transaction SHMM

(Courtesy Dany Charbonneau)


Abap KC:http://www.richard-harper.me.uk/Kb

Post Reply