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

class, and once you have that you can accessthe root class within the broker to get

your monster data.

DATA: lt_monsters TYPE STANDARD TABLE OF ztvc_monsters,

lo_monster TYPE REF TO zcl_monster_shmo.

DO 10 TIMES.

TRY.

"We create an instance of the broker class which

"contains within it a reference to the instance of

"the root class which lives in shared memory

"A (non exclusive) read lock is also set

lo_monster = zcl_monster_shmo=>attach_for_read( ).

"The root class contains our custom methods

lt_monsters[] = lo_monster->root->get_monsters( ).

"Release read lock ; ending the transaction would

"have the same result

lo_monster->detach( ).

EXIT."From Do-End-Do

CATCH cx_shm_no_active_version.

“The root instance has not been created – the BUILD

“method will be called asynchronously

"If after ten seconds the object has not initialised itself we

"give up

WAIT UP TO 1 SECONDS. "Takes a while to load up

CONTINUE. "With do-end-do

CATCH cx_shm_inconsistent "Root class is out of date

cx_shm_exclusive_lock_active "Someone trying to change

cx_shm_change_lock_active "Someone trying to change data

cx_shm_read_lock_active. "Amazingly, this can be an error

EXIT. "From do-end-do, we will use a real database read

ENDTRY.

ENDDO.

Listing 14.4 A Shared Memory Read Request

As is normal in programming, you can see that the bulk of the code in Listing 14.4

is in regard to error handling. There are two possible error situations:

왘 Another user may have the instance of the root class locked in such a way that

you’re not allowed access; someone may bein the middle of changing the data,

for example. In this case, it’s better to read the database directly. The calling

class is in the persistency layer, so reading from the database is not outside its

job description.

644

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

Saved successfully!

Ooh no, something went wrong!