Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Creating Objects and Values →  Shared Objects 

Shared Objects

This example demonstrates the use of shared objects.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_shared_objects.

CLASS demo_flight_list_handler DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-DATA flight_list_handler
    TYPE REF TO demo_flight_list_handler.
    CLASS-METHODS class_constructor.
    METHODS get_flight_list
      RETURNING
        VALUE(flights) TYPE REF TO spfli_tab
      RAISING
       cx_no_flights.
  PRIVATE SECTION.
    DATA area_handle TYPE REF TO cl_demo_flights.
    METHODS create_flight_list
      RAISING
        cx_shm_attach_error
        cx_no_flights.
ENDCLASS.

CLASS demo_flight_list_handler IMPLEMENTATION.
  METHOD class_constructor.
    CREATE OBJECT flight_list_handler.
  ENDMETHOD.
  METHOD get_flight_list.
    DATA flight_list TYPE REF TO cl_demo_flight_list.
    IF area_handle IS INITIAL.
      TRY.
          area_handle = cl_demo_flights=>attach_for_read( ).
        CATCH cx_shm_attach_error.
          TRY.
              me->create_flight_list( ).
              area_handle = cl_demo_flights=>attach_for_read( ).
            CATCH cx_shm_attach_error.
              CREATE OBJECT flight_list.
              flight_list->set_flight_list( ).
          ENDTRY.
      ENDTRY.
    ENDIF.
    IF area_handle IS NOT INITIAL.
      flights = REF #( area_handle->root->flight_list ).
    ELSEIF flight_list IS NOT INITIAL.
      flights = REF #( flight_list->flight_list ).
    ELSE.
      RAISE EXCEPTION TYPE cx_no_flights.
    ENDIF.
  ENDMETHOD.
  METHOD create_flight_list.
    DATA: flight_list  TYPE REF TO cl_demo_flight_list,
          exc_ref      TYPE REF TO cx_no_flights.
    DATA(area_handle) = cl_demo_flights=>attach_for_write( ).
    CREATE OBJECT flight_list AREA HANDLE area_handle.
    area_handle->set_root( flight_list ).
    TRY.
        flight_list->set_flight_list( ).
      CATCH cx_no_flights INTO exc_ref.
        area_handle->detach_rollback( ).
        RAISE EXCEPTION exc_ref.
    ENDTRY.
    area_handle->detach_commit( ).
  ENDMETHOD.
ENDCLASS.

CLASS shm_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.
CLASS shm_demo IMPLEMENTATION.
  METHOD main.
    DATA: flight_list_handler TYPE REF TO demo_flight_list_handler,
          flight_list TYPE REF TO spfli_tab.
    flight_list_handler =
      demo_flight_list_handler=>flight_list_handler.
    TRY.
        flight_list = flight_list_handler->get_flight_list( ).
      CATCH cx_no_flights.
         cl_demo_output=>display_text( 'No flight list available' ).
        RETURN.
    ENDTRY.
    cl_demo_output=>display_data( flight_list->* ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  shm_demo=>main( ).

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

Brokers 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 shared lock for an area instance. Within an internal session, only one shared 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 exclusive 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 removed explicitly, the program is terminated at the end of the current internal session or possibly even beforehand.

If the area was built, get_flight_list attempts to set a shared 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 exclusive lock in create_flight_list is closed explicitly, whereas a shared lock in get_flight_list is persisted until the end of the internal session. The latter point (shared 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 method main creates an object of the 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.