Shared Objects Abap Exercises
Shared Objects Abap Exercises
Exercises / Solutions
With shared objects in ABAP it is possible to share common data on one application server of a SAP system between different user
session. In the first exercise you will define your own shared object area and use it to share sample data between two different
programs. In the second exercise you will adapt the shared object area for high availability and automatic preloading.
All exercises are done by a team of two. Because all teams will work within the same SAP system, the names of global objects must
be uniquely for every team. For this reason all global names used in this description will end with XX. The speaker will give every
team a unique number. Please, substitute the XX in all global names with your team number (e.g. ZCL_MY_AREA_XX becomes
ZCL_MY_AREA_01 for team number 1). All object to be created are only system local object so no transport request is necessary.
Exercise 1
In this exercise you will define a shared objects area and an appropriate root data class. The root data class should have at least one
instance attribute of type string and must be shared memory enabled. After this two programs should be written. One program to write
data to the shared objects area and another program to read data from the shared object area. The data can be displayed by using
ABAP list commands (e.g. write). In the second part of this exercise the shared object area should be checked using the shared object
area monitor. Identify your shared object area and use the object browser to browse the data. At the end use the monitor to free your
shared object area.
In the following section all steps will be described and illustrated with screen shots.
SAP TECHED ’03 BASEL SEPTEMBER 30 – OCTOBER 2, 2003 2
Please, substitute XX with your team number. Use the class builder (transaction SE24) to create a new class.
Use the shared object area manager (transaction SHMA) to create a new shared object area.
Type in a description and the data root class (created with step 1).
3. Write a program ZWRITE_AREA_XX to fill the shared object area with data
Use the ABAP editor (transaction SE38) to create the program ZWRITE_AREA_XX.
Select the Type "Executable Program" and the Status "Test Program" and save.
SAP TECHED ’03 BASEL SEPTEMBER 30 – OCTOBER 2, 2003 6
Write the source to attach for write to the area, create the root object, fill some attributes, set the root reference and commit your
changes.
4. Write a program ZREAD_AREA_XX to read and display the data of the shared object area
Use the ABAP editor (transaction SE38) to create the program ZREAD_AREA_XX.
Select the Type "Executable Program" and the Status "Test Program" and save .
SAP TECHED ’03 BASEL SEPTEMBER 30 – OCTOBER 2, 2003 8
Write the source to attach for read to the area, read and display some attributes and detach from area.
Now you should get exactly the output of data created by the program ZWRITE_AREA_XX.
SAP TECHED ’03 BASEL SEPTEMBER 30 – OCTOBER 2, 2003 9
5. Use the shared object area monitor to identify your shared object area ZCL_SHM_AREA_XX
Use the shared object area monitor (transaction SHMM) to identify your shared object area.
Double click on the area entry to navigate to the default instance and select the default instance (name of the default instance is
$DEFAULT_INSTANCE$).
SAP TECHED ’03 BASEL SEPTEMBER 30 – OCTOBER 2, 2003 10
Now you can browse through the shared object area instance by clicking on complex attributes.
7. Delete your shared object area instance using the shared object area monitor
Go back to the start screen of the shared object area monitor by clicking the icon twice. In the area view select your area again.
Now you have completed the first exercise. If you have still some time left, try to use internal tables and object references as instance
attributes of your root data class..
After the second part of the presentation the exercise 2 should be accomplished.
Exercise 2
In this exercise some tests should be done. The shared object area should be adapted to pass the tests. This exercise is divided into two
parts.
First (2a) the result of running a reader (program ZREAD_AREA_XX) and writer (program ZWRITE_AREA_XX) using the same
shared object area at the same time should be inspected. To extend the read attach time in ZREAD_AREA_XX the ABAP command
WAIT should be used. The result of this test should be that the program ZWRITE_AREA_XX terminates with an exception.
Versioning should be used to make it possible to run both programs without exception.
In the second part (2b) a reader without preceding writer should be inspected. After deleting your area with the shared object area
monitor starting the program ZREAD_AREA_XX will cause an exception. Automatic preloading and autostart at read request should
be used to read the shared object area just with the program ZREAD_AREA_XX. Use the root data class as preloading class and
adapt ZREAD_AREA_XX. The first read attach will fail because there is no active version but the loader was started. As soon as the
loader is finished the read attach should be successful (use the ABAP command WAIT to wait a second for the next try of a read
attach in program ZREAD_AREA_XX).
In the following two sections all necessary steps will be described and illustrated with screen shots.
SAP TECHED ’03 BASEL SEPTEMBER 30 – OCTOBER 2, 2003 12
Exercise 2a
For this exercise the program ZREAD_AREA_XX should be adapted. The runtime of ZREAD_AREA_XX should be extended to
make it possible to run the program ZREAD_AREA_XX and ZWRITE_AREA_XX at the same time. Use the ABAP statement
WAIT UP TO 60 SECONDS to extend the runtime of ZREAD_AREA_XX.
Now start first the program ZREAD_AREA_XX and within the next 60 seconds start the program ZWRITE_AREA_XX in a new
session. The result should be that the program ZWRITE_AREA_XX terminates with the exception
CX_SHM_VERSION_LIMIT_EXCEEDED (the short dump UNCAUGHT_EXCEPTION occurred because the exception
CX_SHM_VERSION_LIMIT_EXCEEDED was not caught).
Now the properties of ZCL_SHM_AREA_XX should be adapted that the short dump above does not occur any longer. Use the shared
object area manager (transaction SHMA) to switch on versioning for your area (activate checkbox Versioning).
Save ZCL_SHM_AREA_XX and repeat starting ZREAD_AREA_XX and ZWRITE_AREA_XX. Now the exception should not
occur anymore.
SAP TECHED ’03 BASEL SEPTEMBER 30 – OCTOBER 2, 2003 14
Exercise 2b
Before starting exercise 2b delete all previous instances of you shared object area (see exercise 1 step 7) and remove the WAIT
statement from program ZREAD_AREA_XX. After this start program ZREAD_AREA_XX. The program ZREAD_AREA_XX
should terminate with the exception CX_SHM_NO_ACTIVE_VERSION (short dump UNCAUGHT_EXCEPTION occurred).
Now automatic preloading should be used to enable the writing of data into the shared object area at read request.
First a preloading class should be defined. In this exercise the root data class ZCL_SHM_AREA_ROOT_XX is used. This class
should implement the interface IF_SHM_BUILD_INSTANCE. Use the class builder (transaction se24) to edit
ZCL_SHM_AREA_ROOT_XX.
SAP TECHED ’03 BASEL SEPTEMBER 30 – OCTOBER 2, 2003 15
Save and activate your root data class. Now the properties of the shared object area ZCL_SHM_AREA_XX should be adapted in this
way that the class ZCL_SHM_AREA_ROOT_XX is used as preloading class furthermore Automatic Preloading and Autostart for
Read Request should be activated.
The last step of this exercise is to adapt the program ZREAD_AREA_XX for automatic preload at read request. Because the method
ATTACH_FOR_READ do not wait until the automatic preloading has finished, the program has to wait for a second and try again.
The first time the method ATTACH_FOR_READ will finish with the exception CX_SHM_NO_ACTIVE_VERSION. The first time
the exception should be caught.
Now every time the program ZREAD_AREA_XX is started and there is no active version, the method
IF_SHM_BUILD_INSTANCE~BUILD of the class ZCL_SHM_AREA_ROOT_XX is automatically called. The second try for a
read attach should always return a valid handle.
Now you have finished the last exercise for this work shop you may try just one other thing. Use the shared object area monitor to
delete your shared object area and start the program ZREAD_AREA_XX again.
SAP TECHED ’03 BASEL SEPTEMBER 30 – OCTOBER 2, 2003 17
No part of this publication may be reproduced or transmitted in any form or for any purpose without the
express permission of SAP AG. The information contained herein may be changed without prior notice.
Some software products marketed by SAP AG and its distributors contain proprietary software components
of other software vendors.
Microsoft®, WINDOWS®, NT®, EXCEL®, Word®, PowerPoint® and SQL Server® are registered
trademarks of Microsoft Corporation.
IBM®, DB2®, DB2 Universal Database, OS/2®, Parallel Sysplex®, MVS/ESA, AIX®, S/390®, AS/400®,
OS/390®, OS/400®, iSeries, pSeries, xSeries, zSeries, z/OS, AFP, Intelligent Miner, WebSphere®,
Netfinity®, Tivoli®, Informix and Informix® Dynamic ServerTM are trademarks of IBM Corporation in
USA and/or other countries.
ORACLE® is a registered trademark of ORACLE Corporation.
UNIX®, X/Open®, OSF/1®, and Motif® are registered trademarks of the Open Group.
Citrix®, the Citrix logo, ICA®, Program Neighborhood®, MetaFrame®, WinFrame®, VideoFrame®,
MultiWin® and other Citrix product names referenced herein are trademarks of Citrix Systems, Inc.
HTML, DHTML, XML, XHTML are trademarks or registered trademarks of W3C®, World Wide Web
Consortium, Massachusetts Institute of Technology.
JAVA® is a registered trademark of Sun Microsystems, Inc.
JAVASCRIPT® is a registered trademark of Sun Microsystems, Inc., used under license for technology
invented and implemented by Netscape.
MarketSet and Enterprise Buyer are jointly owned trademarks of SAP AG and Commerce One.
SAP, SAP Logo, R/2, R/3, mySAP, mySAP.com, xApps, SAP NetWeaver and other SAP products and
services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP
AG in Germany and in several other countries all over the world. All other product and service names
mentioned are the trademarks of their respective companies.