ABAP COMMIT WORK and ROLLBACK WORK: Transaction Control

Category
ABAP-Statements
Published
Author
Johannes

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

  1. All buffered database changes are written to the database.
  2. All registered UPDATE function modules are executed.
  3. The transaction buffer is cleared.
  4. A new LUW begins.

Variants

VariantDescription
COMMIT WORKAsynchronous commit. The program continues immediately.
COMMIT WORK AND WAITSynchronous 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

  1. All buffered database changes are discarded.
  2. All registered UPDATE function modules are not executed.
  3. The transaction buffer is cleared.
  4. A new LUW begins.

Example

DATA: lt_orders TYPE TABLE OF zorder.
" Perform multiple operations
INSERT zorder FROM TABLE @lt_orders.
UPDATE zcustomer SET last_order = @sy-datum
WHERE id = '1001'.
" Check for errors
IF sy-subrc <> 0.
" On error: Undo ALL changes
ROLLBACK WORK.
WRITE: / 'Transaction aborted.'.
RETURN.
ENDIF.
" All OK: Save
COMMIT WORK.

System Fields

After COMMIT WORK:

  • sy-subrc:
    • 0: Commit successful (with AND WAIT).
    • With asynchronous commit without AND WAIT, sy-subrc is always 0.

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 ends

SAP 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 executed
COMMIT 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 function
CALL FUNCTION 'Z_UPDATE_CUSTOMER' IN UPDATE TASK
EXPORTING
customer = ls_customer.
" Further logic...
" At the end: Execute all postings
COMMIT WORK AND WAIT.

Benefits of Posting

  1. Bundling: All changes are executed together.
  2. Error handling: On error, the entire LUW is rolled back.
  3. 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) or X (exit)

Caution: This can cause unintended commits!

" CAUTION: Implicit commit!
INSERT zcustomer FROM @ls_customer.
" This call triggers an implicit COMMIT
CALL 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 1
INSERT zcustomer FROM @ls_customer.
IF sy-subrc <> 0.
lv_error = abap_true.
ENDIF.
" Operation 2
IF 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.
" Decision
IF 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 lock
CALL 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 changes
UPDATE zcustomer FROM @ls_customer.
" Commit
COMMIT WORK AND WAIT.
" Release lock
CALL FUNCTION 'DEQUEUE_EZCUSTOMER'
EXPORTING
id = ls_customer-id.

COMMIT WORK vs. COMMIT WORK AND WAIT

AspectCOMMIT WORKCOMMIT WORK AND WAIT
ExecutionAsynchronousSynchronous
Wait timeNoneWaits for completion
sy-subrcAlways 0Shows success/error
Use caseBackground, when result is not criticalDialog, when result must be checked
" Asynchronous: Faster, but no feedback
COMMIT WORK.
" Synchronous: Slower, but with error checking
COMMIT WORK AND WAIT.
IF sy-subrc <> 0.
" Error handling
ENDIF.

Common Errors

1. Forgotten COMMIT

" WRONG: No COMMIT
INSERT 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 iteration
LOOP AT lt_customers INTO DATA(ls_cust).
INSERT zcustomer FROM @ls_cust.
COMMIT WORK. " Very inefficient!
ENDLOOP.
" BETTER: One COMMIT at the end
INSERT 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 committed
ROLLBACK WORK.

Summary

StatementEffect
COMMIT WORKSaves all changes asynchronously
COMMIT WORK AND WAITSaves all changes synchronously (with feedback)
ROLLBACK WORKDiscards all changes since last COMMIT

Important Notes / Best Practice

  • Execute COMMIT WORK only at logical transaction boundaries, not after every single operation.
  • Use COMMIT WORK AND WAIT when 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 WORK at the end.
  • In update function modules (IN UPDATE TASK), never use COMMIT WORK or ROLLBACK WORK.
  • Use INSERT, UPDATE, DELETE for actual database changes.