14.01.2020 Views

ABAP_to_the_Future

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

14

Shared Memory

RAISE EXCEPTION TYPE cx_shm_build_failed

EXPORTING

previous = lo_exception.

ENDTRY.

"Read the database and fill up the internal table

TRY.

"The broker class is sometimes called a "handle"

"as in 'Handle and Gretel'

CREATE OBJECT lo_root AREA HANDLE lo_broker.

lo_broker->set_root( lo_root ).

lo_root->load_monsters( ).

lo_broker->detach_commit( ).

CATCH cx_shm_error INTO lo_exception.

RAISE EXCEPTION TYPE cx_shm_build_failed

EXPORTING

previous = lo_exception.

ENDTRY.

IF invocation_mode = cl_shm_area=>invocation_mode_auto_build.

CALL FUNCTION 'DB_COMMIT'.

ENDIF.

ENDMETHOD."Build

Listing 14.6 Coding the BUILD Method

Most of the code in Listing 14.6 is stra ightforward enough, but notice how the

instance of the root class is created. Instead of a normal CREATE OBJECT statement,

which creates the instance in the memory of the running prog ram, there is the

AREA HANDLE addition, which means that instead the instance is created in shared

memory and stays there even after the calling program ends.

The other point to note is the DETACH_COMMIT method. For a transactional shared

memory area (such as the one in this example), the shared memory area only gets

updated once a COMMIT WORK occurs, which makes updating the shared memory

part of a logical unit of work fall under the “all shall change or none shall change”

principle. For a nontransactional area, DETACH_COMMIT updates the shared memory

at once.

In any event, as can be seen during a build, a DB_COMMIT function is called to

ensure that both types of shared memory area are updated. The build is running

in a separate session, so the COMMIT can have no side effects.

646

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!