How to create Shared Memory in SAP?

Creating Shared Memory

Call transaction SE24; enter a suitable name to your root class, as shown in the following screenshot. 

On the Properties tab, we need to make sure that the Shared-Memory checkbox is switched on.




We have named it ZCL_MY_ROOT. We will then define two Instance Attributes,

NUMBER and NAME, having private visibility, as shown in the following screenshot:



Two suitable methods, SET_DATA and GET_DATA, are also added to the class. 

The SET_DATA method contains code that imports number and name and assigns to the attributes 

NUMBER and NAME of the class. The GET_DATA method does just the opposite, that is, it 

exports the NUMBER and NAME attribute for a given shared memory object.

Next, the shared memory area should be created. 

This is done via transaction SHMA.

Enter a suitable name and click on the Create button. 

We have typed the name ZCL_MY_EMP_AREA. On the screen that appears, enter the description 

of the area. Also, enter the name of the root class created earlier in the Root Class field. 

You may leave the Client-Specific Area checkbox unchecked as it is not required for our recipe. 

Now, save your entries. Refer to the following screenshot:




This will also generate an area class by entering the same name ZCL_MY_EMP_AREA.



This area class will contain the necessary methods used for reading, changing, and creating the area, 

such as ATTACH_FOR_UPDATE, ATTACH_FOR_READ, and ATTACH_FOR_WRITE.


For creating the set of code that writes object's contents to the shared memory, follow these steps:

1. Two object references my_handle and my_root are defined, one for area class and the other for root class.

2. The static method attach_for_write of the area class zcl_my_emp_area is called.

3. The CREATE OBJECT with the area handle, my_handle must then be called.

4. The root and the created area instance must be linked using the set_root method of the handle.

5. The set_data method is called with the relevant number and name.

6. The detach_commit method of the area class is then called.



The read program is somewhat similar. However, instead of the attach_for_write method used 

earlier, we will use attach_for_read. 

The same instance name is passed and the handle is received. 

The method imposes a read lock on the area instance. Then, the get_data method of the root object is 

called using the area handle, my_handle. This returns the employee name and number stored earlier 

into the variables name and number respectively.

Finally, the detach method is called and the read lock is released.




While creating the shared memory area, if we select the Transactional Area checkbox, the area 

becomes transactional. In this case, the modifications to the area instance versions are not active 

immediately after the call of detach_commit method. Rather, they become active when the next 

database commit is executed.



No comments:

Post a Comment