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

Shared Objects

This example demonstrates the use of shared objects.

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

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.