Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP - Database Accesses →  Object Services →  Persistence Service 

Persistence Service

This example demonstrates creating a persistent object.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA wa_spfli TYPE spfli.

    DATA: connection TYPE REF TO cl_spfli_persistent,
          agent      TYPE REF TO ca_spfli_persistent.

    DATA: exc TYPE REF TO cx_root,
          text TYPE string.

    wa_spfli-carrid     = 'LH'.
    wa_spfli-connid     = '123'.
    wa_spfli-countryfr  = 'DE'.
    wa_spfli-cityfrom   = 'FRANKFURT'.
    wa_spfli-airpfrom   = 'FRA'.
    wa_spfli-countryto  = 'SG'.
    wa_spfli-cityto     = 'SINGAPORE'.
    wa_spfli-airpto     = 'SIN'.
    wa_spfli-fltime     = '740'.
    wa_spfli-deptime    = '234500'.
    wa_spfli-arrtime    = '180000'.
    wa_spfli-distance   = '10000'.
    wa_spfli-distid     = 'KM'.
    wa_spfli-fltype     = ' '.
    wa_spfli-period     = '1'.

    agent = ca_spfli_persistent=>agent.

    TRY.
        connection = agent->get_persistent( i_carrid = wa_spfli-carrid
                                           i_connid = wa_spfli-connid
                                          ).
        MESSAGE 'Connection already exists' TYPE 'I'.
        CALL SELECTION-SCREEN 400 STARTING AT 10 10.
        IF delete = 'X'.
          TRY.
              agent->delete_persistent( i_carrid = wa_spfli-carrid
                                       i_connid = wa_spfli-connid ).
              COMMIT WORK.
            CATCH cx_root INTO exc.
              text = exc->get_text( ).
              MESSAGE text TYPE 'I'.
          ENDTRY.
        ENDIF.
      CATCH cx_root INTO exc.
        text = exc->get_text( ).
        MESSAGE text TYPE 'I'.
        TRY.
            connection = agent->create_persistent(
                           i_carrid = wa_spfli-carrid
                           i_connid = wa_spfli-connid
                           i_countryfr = wa_spfli-countryfr
                           i_cityfrom = wa_spfli-cityfrom
                           i_airpfrom = wa_spfli-airpfrom
                           i_countryto = wa_spfli-countryto
                           i_cityto = wa_spfli-cityto
                           i_airpto = wa_spfli-airpto
                           i_fltime = wa_spfli-fltime
                           i_deptime = wa_spfli-deptime
                           i_arrtime = wa_spfli-arrtime
                           i_distance = wa_spfli-distance
                           i_distid = wa_spfli-distid
                           i_fltype = wa_spfli-fltype
                          i_period = wa_spfli-period            ).
            MESSAGE 'Connection created' TYPE 'I'.
            CALL SELECTION-SCREEN 500 STARTING AT 10 10.
            IF commit = 'X'.
              COMMIT WORK.
            ENDIF.
          CATCH cx_root INTO exc.
            text = exc->get_text( ).
            MESSAGE text TYPE 'I'.
        ENDTRY.
    ENDTRY.

Description

A reference to the class actor of the pesistent class CL_SPFLI_PERSISTENT is assigned to the reference variable agent. It is created once by the static constructor of the CA_SPFLI_PERSISTENT class. Using the GET_PERSISTENT method, a check is made to establish whether there is already a persistent object with the required key in the database. If an object already exists, it can be deleted using DELETE_PERSISTENT. If no object exists, the exception CX_OS_OBJECT_NOT_FOUND occurs and is caught. In the corresponding CATCH block, an attempt is made using CREATE_PERSISTENT to generate the object. Note that the object is only generated for the database when the COMMIT WORK statement is used. Without an explicit COMMIT WORK, it exists only as an administrating object in the program and it will be deleted without affecting the database at the end of the program.