The statements COMMIT WORK and ROLLBACK WORK are central to transaction control in ABAP. They determine whether database changes are permanently saved or undone.
Basic Concept: Logical Unit of Work (LUW)
In ABAP, database changes are not immediately saved permanently. Instead, they are collected in a transaction buffer until an explicit decision is made:
COMMIT WORK: All changes are permanently saved to the database.ROLLBACK WORK: All changes since the last COMMIT are discarded.
A Logical Unit of Work (LUW) is the set of all database operations between two COMMIT points. The LUW guarantees that either all changes are saved or none (atomicity).
COMMIT WORK – Save Changes
Syntax
COMMIT WORK [AND WAIT].Behavior
- All buffered database changes are written to the database.
- All registered UPDATE function modules are executed.
- The transaction buffer is cleared.
- A new LUW begins.
Variants
| Variant | Description |
|---|---|
COMMIT WORK | Asynchronous commit. The program continues immediately. |
COMMIT WORK AND WAIT | Synchronous commit. The program waits until all changes are complete. |
Example
DATA: ls_customer TYPE zcustomer.
ls_customer-id = '1001'.ls_customer-name = 'New Company Inc.'.ls_customer-city = 'Berlin'.
" Insert record (not yet permanent)INSERT INTO zcustomer VALUES @ls_customer.
IF sy-subrc = 0. " Save changes permanently COMMIT WORK AND WAIT.
IF sy-subrc = 0. WRITE: / 'Customer successfully created and saved.'. ELSE. WRITE: / 'Error while saving.'. ENDIF.ELSE. " On error: Discard changes ROLLBACK WORK. WRITE: / 'Customer could not be created.'.ENDIF.ROLLBACK WORK – Discard Changes
Syntax
ROLLBACK WORK.Behavior
- All buffered database changes are discarded.
- All registered UPDATE function modules are not executed.
- The transaction buffer is cleared.
- A new LUW begins.
Example
DATA: lt_orders TYPE TABLE OF zorder.
" Perform multiple operationsINSERT zorder FROM TABLE @lt_orders.
UPDATE zcustomer SET last_order = @sy-datum WHERE id = '1001'.
" Check for errorsIF sy-subrc <> 0. " On error: Undo ALL changes ROLLBACK WORK. WRITE: / 'Transaction aborted.'. RETURN.ENDIF.
" All OK: SaveCOMMIT WORK.System Fields
After COMMIT WORK:
sy-subrc:0: Commit successful (withAND WAIT).- With asynchronous commit without
AND WAIT,sy-subrcis always0.
After ROLLBACK WORK:
- No relevant system fields are set.
Database LUW vs. SAP LUW
Database LUW
The database LUW includes all direct database operations (INSERT, UPDATE, DELETE, SELECT) between two COMMIT points.
" Database LUW 1 begins
INSERT zcustomer FROM @ls_customer1.UPDATE zproduct SET price = 100 WHERE id = 'P001'.
COMMIT WORK. " Database LUW 1 ends
" Database LUW 2 begins
DELETE FROM zlog WHERE created < '20240101'.
COMMIT WORK. " Database LUW 2 endsSAP LUW (Logical Unit of Work)
The SAP LUW is an extended concept that also includes UPDATE function modules and posting. It allows bundling database changes and executing them only at the end of a business transaction.
" Register changes (no DB change yet)CALL FUNCTION 'Z_UPDATE_CUSTOMER' IN UPDATE TASK EXPORTING customer = ls_customer.
CALL FUNCTION 'Z_UPDATE_ORDER' IN UPDATE TASK EXPORTING order = ls_order.
" Now all registered functions are executedCOMMIT WORK.UPDATE TASK – Posting
With IN UPDATE TASK, function modules can be registered for asynchronous posting:
Function Module for Posting
FUNCTION z_update_customer.*"----------------------------------------------------------------------*" IMPORTING*" VALUE(customer) TYPE zcustomer*"----------------------------------------------------------------------
" This code is executed at COMMIT WORK MODIFY zcustomer FROM customer.
ENDFUNCTION.Call in Main Program
DATA: ls_customer TYPE zcustomer.
ls_customer-id = '1001'.ls_customer-name = 'Modified Company'.
" Register posting functionCALL FUNCTION 'Z_UPDATE_CUSTOMER' IN UPDATE TASK EXPORTING customer = ls_customer.
" Further logic...
" At the end: Execute all postingsCOMMIT WORK AND WAIT.Benefits of Posting
- Bundling: All changes are executed together.
- Error handling: On error, the entire LUW is rolled back.
- Performance: Asynchronous execution relieves the dialog.
Implicit COMMIT
A COMMIT WORK is automatically executed when:
- End of a dialog step (user action like ENTER, F-key)
- Calling a transaction (
CALL TRANSACTION,LEAVE TO TRANSACTION) - Calling an RFC function module
- Sending a message with type
A(abort) orX(exit)
Caution: This can cause unintended commits!
" CAUTION: Implicit commit!INSERT zcustomer FROM @ls_customer.
" This call triggers an implicit COMMITCALL TRANSACTION 'VA01'.
" The INSERT change is now already committed!" A ROLLBACK WORK here would have no effect.Implicit ROLLBACK
A ROLLBACK WORK is automatically executed when:
- Runtime error (dump)
- Message with type
A(in some contexts) - Program abort
Best Practices for Transactions
1. Error Handling with ROLLBACK
DATA: lv_error TYPE abap_bool VALUE abap_false.
" Operation 1INSERT zcustomer FROM @ls_customer.IF sy-subrc <> 0. lv_error = abap_true.ENDIF.
" Operation 2IF lv_error = abap_false. UPDATE zorder SET customer_id = @ls_customer-id WHERE order_id = @lv_order_id. IF sy-subrc <> 0. lv_error = abap_true. ENDIF.ENDIF.
" DecisionIF lv_error = abap_false. COMMIT WORK AND WAIT. WRITE: / 'Transaction successful.'.ELSE. ROLLBACK WORK. WRITE: / 'Transaction failed.'.ENDIF.2. TRY-CATCH with ROLLBACK
TRY. INSERT zcustomer FROM @ls_customer. IF sy-subrc <> 0. RAISE EXCEPTION TYPE cx_failed_insert. ENDIF.
UPDATE zorder SET status = 'PROCESSED' WHERE customer_id = @ls_customer-id.
COMMIT WORK AND WAIT.
CATCH cx_failed_insert. ROLLBACK WORK. WRITE: / 'Error during insert.'.
CATCH cx_root INTO DATA(lx_error). ROLLBACK WORK. WRITE: / 'Unexpected error:', lx_error->get_text( ).ENDTRY.3. Locks Before Changes
" Request lockCALL FUNCTION 'ENQUEUE_EZCUSTOMER' EXPORTING id = ls_customer-id EXCEPTIONS foreign_lock = 1 OTHERS = 2.
IF sy-subrc <> 0. WRITE: / 'Record is locked.'. RETURN.ENDIF.
" Perform changesUPDATE zcustomer FROM @ls_customer.
" CommitCOMMIT WORK AND WAIT.
" Release lockCALL FUNCTION 'DEQUEUE_EZCUSTOMER' EXPORTING id = ls_customer-id.COMMIT WORK vs. COMMIT WORK AND WAIT
| Aspect | COMMIT WORK | COMMIT WORK AND WAIT |
|---|---|---|
| Execution | Asynchronous | Synchronous |
| Wait time | None | Waits for completion |
| sy-subrc | Always 0 | Shows success/error |
| Use case | Background, when result is not critical | Dialog, when result must be checked |
" Asynchronous: Faster, but no feedbackCOMMIT WORK.
" Synchronous: Slower, but with error checkingCOMMIT WORK AND WAIT.IF sy-subrc <> 0. " Error handlingENDIF.Common Errors
1. Forgotten COMMIT
" WRONG: No COMMITINSERT zcustomer FROM @ls_customer." Change is lost at program end!
" CORRECT:INSERT zcustomer FROM @ls_customer.COMMIT WORK.2. COMMIT in Loop
" BAD: COMMIT in every iterationLOOP AT lt_customers INTO DATA(ls_cust). INSERT zcustomer FROM @ls_cust. COMMIT WORK. " Very inefficient!ENDLOOP.
" BETTER: One COMMIT at the endINSERT zcustomer FROM TABLE @lt_customers.COMMIT WORK.3. Implicit COMMIT Overlooked
INSERT zcustomer FROM @ls_customer.
" CAUTION: RFC causes implicit COMMIT!CALL FUNCTION 'Z_REMOTE_FUNCTION' DESTINATION 'RFC_DEST'.
" ROLLBACK here is ineffective - data is already committedROLLBACK WORK.Summary
| Statement | Effect |
|---|---|
COMMIT WORK | Saves all changes asynchronously |
COMMIT WORK AND WAIT | Saves all changes synchronously (with feedback) |
ROLLBACK WORK | Discards all changes since last COMMIT |
Important Notes / Best Practice
- Execute
COMMIT WORKonly at logical transaction boundaries, not after every single operation. - Use
COMMIT WORK AND WAITwhen you need to check the result. - Implement consistent error handling with
ROLLBACK WORK. - Be aware of implicit COMMITs with transaction calls and RFCs.
- Use SAP locks to avoid data conflicts with parallel access.
- Test critical transactions first with
ROLLBACK WORKat the end. - In update function modules (
IN UPDATE TASK), never useCOMMIT WORKorROLLBACK WORK. - Use
INSERT,UPDATE,DELETEfor actual database changes.