07-10-2019 2:09 PM
dear experts, i have a question about SAP LUW, i am always confused about the SAP LUW rollback mechanism.
As the ABAP document said, no ROLLBACK WORK and COMMIT WORK statement allow under SAP LUW, only message with type A could implicit rollback the SAP LUW, i did below test:
my system component versions are:
i create two tables YLUW1 and YLUW2 like below, and initial the value:
i wrote a report to execute the PERFORM...ON COMMIT statement which is one of the solutions of SAP LUW:
first case, i use message with type A to trigger a implicit rollback and it's worked, but program terminate with error.
result:
second case, i didn't write anything under sy-subrc check, and table YLUW1 inserted successfully, YLUW2 inserted failed.
result:
the last case, i raise a exception to trigger a implicit rollback and it's worked, but program terminate with error.
result:
after all of these, my question is: how i can rollback the SAP LUW when any table insert/update/delete error and i could catch the SAP LUW error then deal with it.
07-11-2019 7:09 AM
Remember that "creating subroutines is obsolete", create update function modules instead. Via update function modules, any RAISE or MESSAGE RAISING or MESSAGE E/A error will be shown as a popup "Express Information Express document "Update was terminated" received from author "user ..." OK - Choose (details) - Inbox", so it's less aggressive than short dumps or Abort popups, and you may handle the errors via SM13, and possibly recover them.
The principle of update tasks is to make all possible checks to avoid database update errors before the COMMIT WORK. If an error occurs inside the update task, it's too late, and only an abort is possible (hopefully it's very rare because of all previous checks).
Here is the pseudo code:
TRY.
do_sap_locks( ).
check_functional_errors_and_database_integrity_1( ).
CALL FUNCTION ... IN UPDATE TASK ...
check_functional_errors_and_database_integrity_2( ).
CALL FUNCTION ... IN UPDATE TASK ...
COMMIT WORK.
CATCH ...
ROLLBACK WORK.
ENDTRY.
Pseudo code of update function modules (message types 'E' or 'A' are possible):
INSERT yluw1 FROM lw_data.
IF sy-subrc <> 0.
MESSAGE e001(00) WITH 'text1' 'text2' RAISING db_update_error.
--or--
MESSAGE e001(00) WITH 'text1' 'text2'.
--or--
RAISE db_update_error.
ENDIF.
07-10-2019 3:25 PM
As you are using only OPEN-SQL statements you actually don't need the whole LUW COMMIT/ROLLBACK but only some database commit/rollback, so execture the OPEN-SQL statements, when each and every statement is okay, call FM DB_COMMIT, in case of any error call DB_ROLLBACK.
If you really want to stick with SAP update LUW processing, don't update in form/method of your transaction program, but call FMs IN UPDATE TASK, then execute a COMMIT WORK. In those updayt FMs raise an Abort 'A' message in case of SQL error.
07-11-2019 8:04 AM
07-10-2019 6:11 PM
Do your checks before doing the commit and thereby triggering update function modules.
07-10-2019 8:20 PM
Hi,
kindly check below document.
Database Logical Unit of Work (LUW)
You can use a TRY ..... ENDTRY to catch the exception
"use with class based exception CX_SY_OPEN_SQL_DB.
DATA: lv_check TYPE REF TO cx_sy_open_sql_db,
lv_text TYPE STRING
TRY.
INSERT zXXXX FROMTABLE gt_itab.
CATCH CX_SY_OPEN_SQL_DB INTO lv_check.
lv_text = lv_check->get_text().
ROLLBACKWORK.
07-10-2019 9:15 PM
07-11-2019 7:09 AM
Remember that "creating subroutines is obsolete", create update function modules instead. Via update function modules, any RAISE or MESSAGE RAISING or MESSAGE E/A error will be shown as a popup "Express Information Express document "Update was terminated" received from author "user ..." OK - Choose (details) - Inbox", so it's less aggressive than short dumps or Abort popups, and you may handle the errors via SM13, and possibly recover them.
The principle of update tasks is to make all possible checks to avoid database update errors before the COMMIT WORK. If an error occurs inside the update task, it's too late, and only an abort is possible (hopefully it's very rare because of all previous checks).
Here is the pseudo code:
TRY.
do_sap_locks( ).
check_functional_errors_and_database_integrity_1( ).
CALL FUNCTION ... IN UPDATE TASK ...
check_functional_errors_and_database_integrity_2( ).
CALL FUNCTION ... IN UPDATE TASK ...
COMMIT WORK.
CATCH ...
ROLLBACK WORK.
ENDTRY.
Pseudo code of update function modules (message types 'E' or 'A' are possible):
INSERT yluw1 FROM lw_data.
IF sy-subrc <> 0.
MESSAGE e001(00) WITH 'text1' 'text2' RAISING db_update_error.
--or--
MESSAGE e001(00) WITH 'text1' 'text2'.
--or--
RAISE db_update_error.
ENDIF.
07-11-2019 8:04 AM