SAP SHARED OBJECTS ABEXA

Get Example source ABAP code based on a different SAP table
  


ARTICLE

Shared Objects
This example demonstrates the use of shared objects.

ABAP_SOURCE_CODE
ABAP_EXEC

ABAP_DESCRIPTION

Area root class
The global class CL_DEMO_FLIGHT_LIST is used as an area root class. It contains the internal table FLIGHT_LIST (of type SPFLI_TAB from the ABAP Dictionary) as a public attribute, which is filled by the method SET_FLIGHT_LIST. This table represents the unchangeable data that is accessed from different programs.

Area
CL_DEMO_FLIGHTS is used as an area, whose properties are maintained in transaction SHMA . The default values were applied apart from two exceptions:
Versioning is deactivated because the data in the area instance should not be changed during the program runtime.
The lifetime after the last access is restricted to five minutes. This prevents memory space from being occupied in the shared memory without a program requiring the memory.

Broker and loaders
The class demo_flight_list_handler encapsulates the access to the area before the client (in the real world, this would be a global class). The static constructor creates one instance of this class (singleton). The method get_flight_list is used as a broker. It attempts to set a read lock for an area instance. During an internal session, only one read lock is possible for an area instance. Therefore the system first queries whether an area handle area already exists. The alternative would have been to handle the exception CX_SHM_READ_LOCK_ACTIVE. However, this would have been less effective in this example. If the operation is not successful, the method create_flight_list is called, which acts as a loader. This attempts to set a write lock and to build an area instance with a root object. Any exceptions that occur are propagated to the calling method. A DETACH_ ROLLBACK is executed before any possible CX_NO_FLIGHTS exceptions from SET_FLIGHT_LIST are forwarded to the calling program. If the change lock is not explicitly removed, the program will be terminated at the end of the current internal session or possibly even beforehand.
If the area was successfully built, get_flight_list attempts to set a read lock again. If the area instance could not be built, get_flight_list creates an object of class ZCL_FLIGHT_LIST (as an emergency measure) in the current internal session and fills the internal table flight_list. Finally, a data reference to the flight list is assigned to the return value of the method (either in the root object of the shared object or in the local object).
The write lock in create_flight_list is explicitly closed, whereas a read lock in get_flight_list is retained until the end of the internal session. The latter point (read lock) is only possible for areas without versioning, if no more data-changing accesses are expected once an area has been built.

Clients
The class shm_demo demonstrates a client. The main method creates an object of class demo_flight_list_handler and attempts to read a reference to the flight list. If the access is successful, the flight list is displayed in the display method. It is not important for the client whether the data is actually contained in the shared memory or (if an error occurs) in a local object of class CL_DEMO_FLIGHT_LIST. If no data can be read from the database, a message is displayed.
After the program call, the area instance version can be examined using transaction SHMM.