Alternative option for IMPORT/ EXPORT using Shared Memory SHMA approach.

Motivation:-

In this blog you will learn how to use shared memory as alternative option of ABAP Memory(Import/Export).

Business Scenario:- Many of times we need Import & Export at the time of SAP Enhancement or other requirements so Here use of shared memory which is also alternate option of Import/ Export and as per SAP standard guideline this approach is highly recommended.

Class%20Creation%20with%20Shared%20Memory

Class Creation with Shared Memory

Add this Interface IF_SHM_BUILD_INSTANCE in Interface

Adding%20Interface

Create any table as per your requirement- in my case I have created a range table.

Assignment%20of%20Internal%20Table

Assignment of Internal Table

Internal%20Table

Internal Table

Save and Activated

Open T-Code – SHMA , Give any name and create

T-Code%20SHMA

T-Code SHMA

T-Code%20-%20SHMA%20inside%20Screen

T-Code – SHMA inside Screen

Give below setting as per the screen shot.

SHMA%20Setting

SHMA Setting

Now Run T-Code – SE24 and open class ZCL_ROOT_SHMA in the change mode and double click on SHMA and double click on Method – IF_SHM_BUILD_INSTANCE~BUILD.

method IF_SHM_BUILD_INSTANCE~BUILD.

 data: lr_area TYPE REF TO ZCL_ROOT_SHMA_USE,
       lr_root type REF TO ZCL_ROOT_SHMA,
       lr_exp type REF TO cx_root.

  try.
  lr_area = ZCL_ROOT_SHMA_USE=>attach_for_write( ).

  CATCH cx_shm_error INTO lr_exp.
  RAISE EXCEPTION TYPE cx_shm_build_failed
  EXPORTING
    previous =   lr_exp.
  ENDTRY.


  CREATE OBJECT lr_root AREA HANDLE lr_area.
  lr_area->set_root( lr_root ).
  lr_area->detach_commit( ).
  endmethod.

Save and Activate the above code.

Now below code Report A & Report B ,I have created as example , Here will export internal table in the shared memory.

Report A-
TRY.
  zcl_root_shma_use=>build( ).
  CATCH cx_shma_not_configured. “ SHM Administration: Area Property Is Not Configured
  CATCH cx_shma_inconsistent.   “ SHM Administration: Inconsistent Attribute Combination
  CATCH cx_shm_build_failed.    “ Constructor Run Failed

ENDTRY.

TRY.
    DATA(lr_area) = zcl_root_shma_use=>attach_for_update(
*                 inst_name   = cl_shm_area=>default_instance
*                 attach_mode = cl_shm_area=>attach_mode_default
*                 wait_time   = 0
                   ).
  CATCH cx_shm_inconsistent.           “ Different Definitions Between Program and Area
  CATCH cx_shm_no_active_version.      “ No active version exists for an attach
  CATCH cx_shm_exclusive_lock_active.  “ Instance Already Locked
  CATCH cx_shm_version_limit_exceeded. “ No Additional Versions Available
  CATCH cx_shm_change_lock_active.     “ A write lock is already active
  CATCH cx_shm_parameter_error.        “ Passed parameter has incorrect value
  CATCH cx_shm_pending_lock_removed.   “ Shared Objects: Waiting Lock Was Deleted

ENDTRY.

DATA lr_root TYPE REF TO zcl_root_shma.
TRY.
    lr_root ?= lr_area->get_root( ).
  CATCH cx_shm_already_detached.
ENDTRY.


DATA wa_kunnr TYPE range_vbeln.
DATA gt_kunnr TYPE TABLE OF range_vbeln.
wa_kunnr-sign = ‘I’.
wa_kunnr-option = ‘EQ’.
wa_kunnr-low  = ‘12345’.
wa_kunnr-high = ‘9009000’.
APPEND wa_kunnr TO gt_kunnr.

lr_root->lt_kunnr = gt_kunnr.
lr_area->set_root( lr_root ).
lr_area->detach_commit( ).

 

Now Use below code for Import Internal table from shared Memory

Report B-
TRY.
    DATA(lr_area) = zcl_root_shma_use=>attach_for_read( ).
  CATCH cx_shm_inconsistent.          " Different Definitions Between Program and Area
  CATCH cx_shm_no_active_version.     " No active version exists for an attach
  CATCH cx_shm_read_lock_active.      " Request for a Second Read Lock
  CATCH cx_shm_exclusive_lock_active. " Instance Already Locked
  CATCH cx_shm_parameter_error.       " Incorrect parameter passed
  CATCH cx_shm_change_lock_active.    " A Change Lock Is Already Active

ENDTRY.

DATA LR_ROOT TYPE REF TO zcl_root_shma.
TRY.
LR_ROOT ?= LR_AREA->get_root( ).
          CATCH cx_shm_already_detached. " Handle Already Released

ENDTRY.
DATA  GT_kunnr type TABLE OF RANGE_VBELN.
GT_kunnr = LR_ROOT->lt_kunnr.

LR_AREA->detach_area( ).

 

Run Report A and see the Report B output in Debug mode

Debug%20mode%20for%20Report%20B

Debug mode for Report B

Captured%20data%20in%20the%20Internal%20table%20from%20Shared%20Memory

Captured data in the Internal table from Shared Memory

Summary:

This blog can help you to understand how to use shared memory class and how can we use as alternative option of Export/Import.

Hope this blog may help you in your use cases. Please do like follow and comment if you have an any query on this topic.

Thanks

Dinesh Kumar

Scroll to Top