Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  Object Services →  Persistence Service 

Persistence Service

This example demonstrates how a persistent object is created.

Other versions: 7.31 | 7.40 | 7.54

Source Code

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

    DATA(agent) = ca_spfli_persistent=>agent.
    TRY.
        DATA(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 DATA(exc).
              MESSAGE exc->get_text( ) TYPE 'I'.
          ENDTRY.
        ENDIF.
      CATCH cx_root INTO exc.
        MESSAGE exc->get_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.
            MESSAGE exc->get_text( ) TYPE 'I'.
        ENDTRY.
    ENDTRY.

Description

A reference to the class agent of the persistent 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 method GET_PERSISTENT, 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 is raised and caught. In the corresponding CATCH block, an attempt is made to create the object using CREATE_PERSISTENT. Note that the object is only created on the database when the statement COMMIT WORK is used. Without an explicit COMMIT WORK, it exists only as an administered object in the program and it is deleted without affecting the database at the end of the program.