This recipe shows how to store the instances of your classes in the shared memory of the application server. A number of programs may access these objects that reside on the application server shared memory.
Two classes are necessary for shared memory, namely the area
class and the area root
class. The root
class is necessary for storing (encapsulating) the data that are to be stored in the shared memory. An area
class may comprise of various instances that may consist of a number of versions.
An important concept shown in this recipe is the CREATE OBJECT
statement with the addition AREA HANDLE
. This will create the object in the application server that is shared memory pointed to by the area handle myarea
.
Prior to writing the code for storing objects in shared memory, an area root
class must be created and a shared memory area be defined using transaction SHMA
.
The steps required for creating a root class are:
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.Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com . If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you
ZCL_MY_ROOT
. We will then define two Instance Attributes, NUMBER and NAME, having private visibility, as shown in the following screenshot:SHMA
.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:area
class by entering the same name ZCL_MY_EMP_AREA
.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:
my_handle
and my_root
are defined, one for area
class and the other for root
class.attach_for_write
of the area
class zcl_my_emp_area
is called.CREATE OBJECT
with the area handle, my_handle
must then be called.set_root
method of the handle.set_data
method is called with the relevant number and name.detach_commit
method of the area
class is then called.In the shared memory-writing program, the statements collectively make the writing of object in the shared memory. Let us see how the program code works.
An area instance version needs to be created before any data may be written in the shared memory on the application server. The attach_for_write
static method is used for this purpose and returns a handle to the area instance created in the application server memory. This imposes write
lock on the version.
The CREATE OBJECT
statement is then called with the name of the created handle. This creates a root
object in the area
instance of the shared memory. The link between the area
instance and the root
class is created using the set_root
method. The set_data
method is then called for the root reference my_root
and supplied with the name and number of the employee, which are then stored in the shared area. Finally, the detach_commit
method is called and the write
lock is released.
Once the program has run successfully, you may see the created object in the shared memory using the shared memory transaction SHMM
. This will appear as your area class name ZCL_MY_EMP_AREA. Refer to the following screenshot:
Double-click on the name of area to view the details, as shown in the following screenshot:
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.
3.137.172.115