Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SAP ABAP Advanced Cookbook

You're reading from   SAP ABAP Advanced Cookbook Featuring over 80 sophisticated recipes, this is a superb tutorial for ABAP developers and consultants. It teaches you advanced SAP programming using the high level language through diagrams, step-by-step instructions, and real-time examples.

Arrow left icon
Product type Paperback
Published in Dec 2012
Publisher Packt
ISBN-13 9781849684880
Length 316 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Rehan Zaidi Rehan Zaidi
Author Profile Icon Rehan Zaidi
Rehan Zaidi
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

SAP ABAP Advanced Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. ABAP Objects 2. Dynamic Programming FREE CHAPTER 3. ALV Tricks 4. Regular Expressions 5. Optimizing Programs 6. Doing More with Selection Screens 7. Smart Forms – Tips and Tricks 8. Working with SQL Trace 9. Code Inspector 10. Simple Transformations 11. Sending E-mail Using BCS Classes 12. Creating and Consuming Web Services 13. SAP Interactive Forms by Adobe 14. Web Dynpro for ABAP 15. Floorplan Manager Index

Creating a shared memory object


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.

Getting ready

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:

  1. 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.

    Tip

    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

  2. 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:

  3. 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.

  4. Next, the shared memory area should be created. This is done via transaction SHMA.

  5. 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:

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

  7. 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.

How to do it...

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.

How it works...

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:

There's more...

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image