Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Transactional BGRFC Delay Method

former_member202335
Participant
2,408

Hello All,

I work as an ABAPer but don't have a lot of experience with BGRFCs.

Requirement : -
  1. The user makes amendments in a document called 'Trading Contract' and clicks on the 'Save' button to save them.
  2. When the save button is clicked, a BGRFC is executed to transition (via a BAPI) the contract from its existing status to a new status ('Awaiting Amendment Approval'). A BGRFC is being used because this can't be done in the same LUW because of technical reasons.

Program snippet: -

....
lv_dest_name = '/ACCGO/AMEND'.
lo_destination = cl_bgrfc_destination_inbound=>create( lv_dest_name ).
lo_unit = lo_destination->create_trfc_unit( ).

CALL FUNCTION '/ACCGO/CTR_TC_STATUS_CHANGE' IN BACKGROUND UNIT lo_unit
EXPORTING
iv_tkonn = iv_contract_num
iv_btbsta = lv_unapp_amend_status.

CALL METHOD lo_unit=>delay(5).
....
....
COMMIT statement is somewhere later in the code.

Issue: -

After the COMMIT, it sometimes takes some time for the lock on the trading contract to get released. If the BGRFC gets immediately executed (after execution of the COMMIT), and if the lock is not released, the BGRFC will not be able to do its job. I introduced the DELAY statement above hoping that it will reduce the chances of this happening. But, as a result of the DELAY, it seems that the unit simply does not get executed...it remains locked (as seen in SBGRFCMON) even after the 5 seconds are over. Could anyone help me out here?

2 REPLIES 2
Read only

Sandra_Rossi
Active Contributor
1,759

Why don't you start your RFC function at the end of the update task to minimize the chance of being locked? (you do this by creating an update function module, and running it after all other update function modules, and your update function module starts the RFC).

Read only

ldaunys
Explorer
0 Likes
1,759

I had exact same problem and realized there's no way to ensure data is unlocked, so what I did is try to acquire the lock for 200 seconds (that is enough for my project) and if that fails I crash the unit with an error (just throw an exception). This allows a human being to look at it and restart it in SBGRFCMON. Units can also be restarted through code (like dedicated programs).

This code is written within a bgRFC function. I launch the unit and let it wait for locks with some delays.

while lo_obj->is_locked = abap_false         <br>   and lv_lock_attempts < 100. "Limit attempts count to prevent lockups
  try.
      add 1 to lv_lock_attempts.
      lo_obj->lock( ). "Throws exception if lock fails
    catch zcx_lock.
        call function 'ENQUE_SLEEP' 
           exporting
             seconds = 2.
  endtry.
endwhile.
...
if lo_obj->is_locked = abap_false.
  raise object_locked. "Crashes the unit
endif.

Note: I also call this unit within the same LUW where unlock is supposed to happen to minimize the time window that people have to manually lock the data. You might also want to use unit->delay( ) if you know for a fact that data will stay locked for at least a specific amount of time.