Application Development 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: 

Rollback after 3 inserts not working

ekekakos
Participant
0 Kudos
461

Hello, I have the following problem. I am trying to update update several Z DB tables with the following code

INSERT zek_xk_lines FROM TABLE db_xk_lines ACCEPTING DUPLICATE KEYS.
IF sy-subrc = 0.
  INSERT zek_xklinkae FROM TABLE db_xk_line_kae ACCEPTING DUPLICATE KEYS.
  IF sy-subrc = 0.
    IF wa_xk_line-xk_traffic_ticket IS NOT INITIAL.
      db_xk_line_traffic = CORRESPONDING #( wa_xk_line-xk_traffic_ticket ).
      INSERT zek_xklinedt FROM TABLE db_xk_line_traffic ACCEPTING DUPLICATE KEYS.
      IF sy-subrc = 0.
        COMMIT WORK AND WAIT.
      ELSE.
        ROLLBACK WORK.
        r_result = |Failed to insert in DB TRAFFIC TICKETS (zek_xklinedt)|.
      ENDIF.
    ENDIF.
    IF wa_xk_line-xk_rent IS NOT INITIAL.
      db_xk_line_rents = CORRESPONDING #( wa_xk_line-xk_rent ).
      INSERT zek_xk_rent FROM TABLE db_xk_line_rents ACCEPTING DUPLICATE KEYS.
      IF sy-subrc = 0.
        COMMIT WORK AND WAIT.
      ELSE.
        ROLLBACK WORK.
        r_result = |Failed to insert in DB RENTS (zek_xk_rents)|.
      ENDIF.
    ENDIF.
    IF wa_xk_line-xk_doses IS NOT INITIAL.
      db_xk_line_doses = CORRESPONDING #( wa_xk_line-xk_doses ).
      INSERT zdae_migt_doses FROM TABLE db_xk_line_doses ACCEPTING DUPLICATE KEYS.
      IF sy-subrc = 0.
        COMMIT WORK AND WAIT.
      ELSE.
        ROLLBACK WORK.
        r_result = |Failed to insert in DB DOSES (zdae_migt_doses)|.
      ENDIF.
    ENDIF.
    IF r_result = ''.
      COMMIT WORK AND WAIT.
    ENDIF.
  ELSE.
    ROLLBACK WORK.
    r_result = |Failed to insert in DB KAE|.
  ENDIF.
ELSE.
  ROLLBACK WORK.
  r_result = |Failed to insert in DB LINE|.
ENDIF.

I noticed that when the INSERT of the 2nd table fails subrc=4 because of the duplicate keys, the ROLLBACK WORK is not working and Commits all the inserted records even for the 2nd one.

Does anyone knows why and what to do?

Thanks

Elias

1 ACCEPTED SOLUTION

jmodaal
Active Contributor
409

Hello,

ROLLBACK WORK does not revert changes in the database that were already committed - is this possibly your assumption? Once executed, a COMMIT WORK effects all updates in the database in the LUW done so far. BTW, if you do not use asynchronous updates you should not use the addition AND WAIT.

In the above given source code I would assume that a final COMMIT WORK resp. ROLLBACK WORK at the end would be more fitting to the needs.

Kind regards

Jan

2 REPLIES 2

jmodaal
Active Contributor
410

Hello,

ROLLBACK WORK does not revert changes in the database that were already committed - is this possibly your assumption? Once executed, a COMMIT WORK effects all updates in the database in the LUW done so far. BTW, if you do not use asynchronous updates you should not use the addition AND WAIT.

In the above given source code I would assume that a final COMMIT WORK resp. ROLLBACK WORK at the end would be more fitting to the needs.

Kind regards

Jan

0 Kudos
409

Thanks Jan. That's what I did.