0% found this document useful (0 votes)
822 views11 pages

ABAP Shared Memory Objects Tutorial With Sample ABAP Code

This document provides an overview of how to use shared memory objects in ABAP. It discusses creating a root class to define the data structure, creating a shared memory area handle class, and writing ABAP programs to set and get data from the shared memory object. Sample ABAP code is provided to demonstrate creating the classes and programs to read from and write to the shared memory object. The document also discusses how to monitor shared memory areas using transaction code SHMM.

Uploaded by

JORGE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
822 views11 pages

ABAP Shared Memory Objects Tutorial With Sample ABAP Code

This document provides an overview of how to use shared memory objects in ABAP. It discusses creating a root class to define the data structure, creating a shared memory area handle class, and writing ABAP programs to set and get data from the shared memory object. Sample ABAP code is provided to demonstrate creating the classes and programs to read from and write to the shared memory object. The document also discusses how to monitor shared memory areas using transaction code SHMM.

Uploaded by

JORGE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

ABAP Shared Memory Objects Tutorial with Sample ABAP

Code
This ABAP Shared Memory Objects tutorial includes sample ABAP code showing how to
code to store data in shared memory objects and read data from shared memory object. By
following the instructions in this step by step ABAP tutorial, developers can create a shared
memory object in SAP. Given sample ABAP codes and sample programs will help developers
to write data or read data from shared memory objects using shared memory programming.

ABAP developers can use following SAP transaction codes for Shared Memory Object
development, debugging and monitoring:
SE80 or SE24 transaction for ABAP W orkbench Class Builder editor to create root class for
data structure.
SHMA SAP transaction for creating Shared Object Area class interface.
SHMM to monitor Shared Memory Areas objects, their memory consumption, and view and
display data contained in shared memory object

Of course ABAP developers can use transaction SE38 to create two ABAP programs where
they set the shared memory object value and read back from shared memory area object.

Steps to Implement ABAP Shared Object Solution

1.) Create a class that represents data structure. (I'll refer this class as root class). Use SE80 or
SE24 tcode

2.) Create a shared memory area class to wrap data class for standard shared memory object
methods. (I'll refer this class as handle class). Use SHMA transaction

3.) ABAP code to write data to shared memory object. For example you can build a simple
ABAP program using SE38 to set shared memory object value.

4.) ABAP code to read data from shared memory object. A similar ABAP report can be written to
read data from shared memory object using SE38.

Naming of the root class and the shared memory area is important. The first part of the names
of both classes should be same. On the other hand root class name should be ending with
"_root" and the shared memory area class name should end with "_area"

In fact this naming simpifies the work of ABAP developer. ABAP programmers will have a better
understanding about the objects they're using to build a SAP shared memory object solution.

Root Class for Shared Memory Object

In this section of the ABAP tutorial, we will create a class with Shared Memory-Enabled option
is selected.
The class name is Z_VBRK_SHM_TABLE_ROOT for the sake of naming standards.

First of all, call SE80 W orkbench transaction code and choose Class / Interface as object type.
Then type the name of the new root class for your shared memory object as seen in below
screen shot.
Press Enter key for next dialog

When your approval is requested to create the mentioned class since it does not exist, click Yes
to continue to create the new root class for shared memory object.

Choose Class option instead of Interface to indicate that you're creating a class not an interface

Press Enter or OK icon button

Provide class description for the shared memory object root class.
Choose instantiation as Public.
Class type is Usual ABAP class and mark Final checkbox.
When you are finished with basic class properties, click Save to continue for the SAP Class
Builder screen.
Be sure that you do not forget to mark Shared Memory-Enabled checkbox on class properties
screen general data section.

Now after we mark the class as shared memory-enabled, we will now create an attribute where
data will be stored and two class methods to set and get shared data in the class attribute.
Now switch to Attributes tab and define a private attribute where data will be stored.
For example we can create an attribute where ABAP developers can store a list of
SAP documentnumbers in VBELN data type.

When I searched the ABAP dictionary for an existing table type for VBELN data type, I found
VBELN_VL_T table type. I'll use VBELN_VL_T table type in this tutorial to create a class
attribute for storing SAP document numbers like invoice numbers or order numbers in a shared
memory object.

After attribute is created, ABAP developers can continue with next step.
In this step, we will create two public methods.
These methods will be used to set data to private attribute and get data (read) from private
attribute of the shared memory object.

SET_VBELN method has import parameter P_VBELN of type VBELN_VL_T which is a table
type for VBELN data element. This method takes internal table of table type VBELN_VL_T to
set as shared memory object data content.

GET_VBELN method has export parameter P_VBELN_TBL of type VBELN_VL_T which is a


table type for document numbers. Using the export method Get_Vbeln, ABAP users will be able
to return shared memory object table for all stored document numbers.
After the developer defines class attribute and set/get methods of the root class, within the
methods following ABAP codes can be used.

METHOD SET_VBELN.
SHM_VBELN_TBL = P_VBELN.
ENDMETHOD.

METHOD GET_VBELN.
P_VBELN_TBL = SHM_VBELN_TBL.
ENDMETHOD.

Set method assigns the input parameter value as the value of shared memory object attribute.
And the Get method returns the attribute value as export parameter. W e will see how we will
call these shared memory enabled class methods within other ABAP programs later in this
ABAP tutorial.

Create Shared Memory Area: Handle Class

This tutorial step demonstrates the creation of the shared memory area.
To create a Shared Memory Area ABAP developers can use SHMA SAP transaction code.
Area name should be Z_VBRK_SHM_TABLE_AREA according to the naming rules which will
help the developers to reference it without any confusion later

When you press Create button, following Create Area screen will be displayed.
Enter the root class name we have created as shared memory root class in Attributes tab basic
properties section. In our tutorial, we have defined the root class name as
Z_VBRK_SHM_TABLE_ROOT in previous steps.
If you look at the details of the above Create Area screen, you will realize that an ABAP
developer can enable versioning with a Shared Area object. It is also possible to define
the maximum shared area object size that will keep on memory in kByte and maximum
number of allowed versions.

After you complete Shared Memory Area properties like deciding if you want to use it
with Versioning is enabled or not, first press Save icon and then press Release button at
top icons menu.

The class category is Area Class (Shared Objects) when you display the shared memory
area using SE80. The Root attribute of the handle class is automatically set to
previously created root class at step 1.
Ensure to set the superclass CL_SHM_AREA in the properties of the Area class

ABAP Programs

The below ABAP report whose source code is given reads shared memory object value.
Root class attach_for_read method must be called before reading shared memory data using
our custom Get_Vbeln method. At the end of the read process, ABAP developers must call
detach() method to free shared memory object.

REPORT Z_READ_SHARED_MEMORY_OBJECT.

DATA lt_vbeln type VBELN_VL_T.


DATA ls_vbeln like LINE OF lt_vbeln.

DATA lr_handle TYPE REF TO Z_VBRK_SHM_TABLE_AREA.

lr_handle = Z_VBRK_SHM_TABLE_AREA=>attach_for_read( ).
lr_handle->root->get_vbeln( IMPORTING P_VBELN_TBL = lt_vbeln ).
lr_handle->detach( ).

In order to update data stored in memory using shared memory objects, developers must
consider using one of attach_for_write() or attach_for_update() methods.
Following ABAP report is demonstrating how to assign shared memory object value.
First of all, using below ABAP a check is done to see if an active version of the shared memory
object exists or not. This check is done by reading the shared memory object.
If cx_shm_no_active_version exception is catched in the ABAP TRY and CATCH syntax, then
ABAP developer should use attach_for_write method while creating the root class object
handle. Otherwise, attach_for_update method can be used during root creation of the root class
handle.

REPORT z_shared_memory_object_sample.

PARAMETERS: pr_vbeln LIKE vbrk-vbeln DEFAULT '1111111111'.


* ABAP program appends p_VBELN parameter value TO Shared Memory Object

DATA lt_vbeln type VBELN_VL_T.


DATA ls_vbeln like LINE OF lt_vbeln.

DATA lr_handle TYPE REF TO z_vbrk_shm_table_area. " Area Class


DATA lr_root TYPE REF TO z_vbrk_shm_table_root. " Root attribute of handle
class. SHM: Model of a Data Clas

******************************************************
* First Try to Read
data excp type ref to cx_shm_no_active_version.
data lf_readerror type c1.

try.
lr_handle = z_vbrk_shm_table_area=>attach_for_read( ).
lr_handle->root->get_vbeln( IMPORTING P_VBELN_TBL = lt_vbeln ).
lr_handle->detach( ).
catch cx_shm_no_active_version into excp.
lf_readerror = 'X'.
endtry.
******************************************************

APPEND pr_vbeln to lt_vbeln. " append input parameter value to internal table

IF lf_readerror = 'X'.
lr_handle = z_vbrk_shm_table_area=>attach_for_write( ).
ELSE.
lr_handle = z_vbrk_shm_table_area=>attach_for_update( ).
ENDIF.
CREATE OBJECT lr_root AREA HANDLE lr_handle.
lr_handle->set_root( lr_root ).

lr_root->set_vbeln( p_vbeln = lt_vbeln ). " custom SET method is executed

lr_handle->detach_commit( ).

At the end of the ABAP code block for storing data in shared memory object, COMMIT in
database must be executed. Since the ABAP developer should free the SAP shared memory
objects, the DETACH process must be executed too.
To detach from shared memory area with saving data in memory variables, detach_commit()
method can be called.

Monitor Shared Memory Area using SHMM Transaction

SAP transaction SHMM is used for monitoring shared memory areas for memory consumption
and to display data stored in shared memory objects.

When you call SHMM tcode, SAP Shared Memory: Areas screen will be displayed. On the
screen on Areas tab which is the default displayed tab, you will see shared memory areas
created on the related SAP system.

Highlight the shared memory area which you want to monitor in detail as follows.

When you double click the shared memory area, instances of the shared area will be displayed
as a list on a new screen.
Choose the default instance by highlighting it and click on Read Active Version icon to display
shared memory object contents.

In a new SAP screen, we will be displayed root object data in different tabs. The default tab is
Explorer. You will see the shared memory object attribute we have created earlier in this tutorial.

Highlight the SAP shared memory class attribute SHM_VBELN_TBL which is a table storing
document numbers. Then double click it to view its stored row values.

You see below, tutorial sample shared memory object has two document numbers stored in its
class attribute.
To summarize, in this ABAP tutorial I tried to give brief information about SAP shared memory
object and how to use shared memory in your ABAP programs. Using the transaction codes
and step by step guide, an ABAP developer can create a sample shared memory root class and
the shared memory area for this class. Then given ABAP reports can be used to write, update
or read shared memory objects.

Common questions

Powered by AI

To create and manage shared memory objects in ABAP, developers follow several key steps: 1) Create a root class that represents the data structure using transaction codes SE80 or SE24, ensuring the Shared Memory-Enabled option is selected . 2) Create a shared memory area class to wrap the root class with standard shared memory object methods using the SHMA transaction . 3) Define attributes for data storage and methods for setting and getting shared data in the class . 4) Use transaction SE38 to implement ABAP code that writes to and reads from the shared memory object, with attention to naming conventions and methods like attach_for_read and detach for memory management .

Versioning in shared memory areas allows for managing different states of data over time. When creating a shared memory area, developers can enable versioning to maintain several versions of the data, which facilitates consistency and rollback capabilities. It allows developers to define maximum shared area size and number of allowed versions, contributing to resource efficiency and better data management . This feature is beneficial for applications requiring high reliability and where data changes frequently .

Developers can monitor and verify the contents of a shared memory object using the SAP transaction SHMM. This tool provides a detailed view of shared memory areas; by highlighting an area and selecting it, developers can access a list of instances and view active versions of stored data. Exploring attributes like SHM_VBELN_TBL reveals document numbers stored during an ABAP session, facilitating verification of memory content and ensuring alignment with expected data states . This process supports effective data management and troubleshooting in shared memory applications .

SAP transaction SHMM is used for monitoring shared memory areas. It allows developers to view memory consumption and display data contained in shared memory objects. By navigating through the SHMM interface, users can highlight and examine shared memory areas and their instances, view active versions of shared memory objects, and inspect specific data like document numbers stored in class attributes . This helps in performance monitoring and data verification for shared memory applications .

In a shared memory object, a class attribute is set up as a private attribute to store specific data, such as SAP document numbers. For example, an attribute of VBELN_VL_T type, which corresponds to document numbers, is created. This attribute is accessed through public methods; the SET_VBELN method imports a table type to set as the attribute value while GET_VBELN exports the stored table for retrieval . This setup enables efficient data storage and access within the shared memory structure .

The attach_for_read method is used to establish a read connection to a shared memory object, allowing data retrieval through methods such as get_vbeln. After reading, the detach method should be called to release the object. If no active version exists, an exception is caught, prompting the use of attach_for_write to create a new write connection instead . This sequence ensures proper memory resource management and accurate data updates .

Adhering to naming conventions is crucial for clarity and consistency in ABAP shared memory development. Root class names should end with '_root' and shared memory area class names with '_area,'. This standardization helps developers easily identify and reference related classes, simplifying program maintenance and reducing the risk of errors in shared memory object operations . Consistent naming also aids in collaboration and code readability .

In an ABAP program, implement shared memory read methods by first establishing a read connection using attach_for_read to access the root class's data. After establishing connection, invoke specific retrieval methods such as get_vbeln to import the data into a local variable. Finally, release the shared memory object with the detach method to manage memory efficiently . This ensures that shared memory access is controlled, lines up with data lifecycle requirements, and conforms to ABAP's best practices for shared memory utilization .

When defining class methods for shared memory object attributes, developers should follow these guidelines: 1) Methods must clearly identify input and output parameters, like using import parameters in set methods and export parameters in get methods. 2) Ensure that parameter types align with the attribute data type, such as the VBELN_VL_T for document numbers. 3) Implement error handling to manage exceptions during read/write processes. 4) Maintain high cohesion and clear naming for methods to enhance code readability and reusability . Careful design of these methods facilitates efficient and secure data manipulation within the shared memory framework .

To ensure data consistency during write operations in shared memory objects, developers use a sequence of methods: they first attempt to read the data with attach_for_read; if the data version is inactive, indicated by an exception, they switch to attach_for_write or attach_for_update methods. The write operation involves creating or updating shared memory using methods like set_root followed by set_vbeln to update attributes. Finally, detach_commit is used to save changes and release resources, ensuring that data integrity is maintained .

You might also like