SAP LUW, Update Tasks and Commit

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

When a clerk approves an expense claim, two things must change together: the status of the claim and its total. If the program writes the first and the system fails before it writes the second, the claim is half approved and nobody can tell why. The database already offers "all or nothing" for a short sequence of statements, but a classic SAP program talks to the user between its statements, and the work is spread over several short pieces of work that the system hands to different processes. SAP therefore added its own rule on top, the SAP LUW (logical unit of work): collect the changes first, then write all of them in one go when the program says COMMIT WORK. This page explains the two kinds of unit of work, the classic tools for collecting changes (update function modules and subroutines registered for the commit), the statements COMMIT WORK and ROLLBACK WORK, and the controlled SAP LUW, a newer mechanism that checks that code obeys the rule. It is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). The update task and PERFORM ... ON COMMIT belong to classic Standard ABAP on premise; in ABAP Cloud the update task is not available and RAP applies the rule for you. After reading, you can follow a classic program that registers update modules, say what happens at its COMMIT WORK, and read an error in the update monitor.

Before you read

Key terms

  • Database LUW: a sequence of database operations that the database finishes completely or not at all, ended by a database commit or rollback.

  • SAP LUW: a business change made of one or more database LUWs that must be committed or rolled back together; it is ended by COMMIT WORK or ROLLBACK WORK.

  • Work process: the operating-system process of the application server that runs one piece of ABAP at a time.

  • Update function module: a function module marked as an update module, in which the database changes are placed so that they run later.

  • Update task: the mechanism that stores registered update modules and runs them when the program commits.

  • Update work process: a work process reserved for running update modules.

  • Bundling technique: a way to collect change requests in an SAP LUW instead of executing them at once.

  • Controlled SAP LUW: a checking mechanism that detects statements and calls that break the rules of the SAP LUW.

  • Transactional phase: one of the two stages, modify and save, that the controlled SAP LUW distinguishes.

Two kinds of unit of work

The word LUW appears twice, and the two meanings must be kept apart.

A database LUW is the database’s own idea: an indivisible sequence of database operations that ends with a database commit (the changes become permanent) or a database rollback (all changes since the start are undone). It knows nothing about SAP.

An SAP LUW is a business process that cannot be divided: it is carried out completely or not at all. The platform documentation says an SAP LUW normally contains several database LUWs, and the reason is how classic dialog programs run. A transaction with three screens is processed by up to three different work processes, because while the system waits for the user the work process can be given another task. Each switch of work process causes an implicit database commit, so each dialog step is its own database LUW. Other things cause an implicit commit too, for example a synchronous or asynchronous remote function call, or an HTTP call made through the Internet Communication Framework. If the program wrote to the application tables in every screen, a failure in screen three would leave the writes of screens one and two permanently in the database.

flowchart LR S1["Screen 1: dialog step, one database LUW"] --> S2["Screen 2: dialog step, one database LUW"] S2 --> S3["Screen 3: dialog step, one database LUW"] S3 --> C{"COMMIT WORK or ROLLBACK WORK?"} C -- COMMIT WORK --> U["Update part: all registered changes in one database LUW"] C -- ROLLBACK WORK --> X["Registered changes discarded"] S1 -. "SAP LUW: from the start of the transaction to the commit" .- C

The rule of the SAP LUW is therefore: during the dialog, do not change the application tables. Register the change requests instead, and let one work process execute them together, in one final database LUW, when the program commits. The documentation lists three techniques for this collecting (bundling): update function modules, background RFC calls (covered with remote calls on a later page), and subroutines registered with PERFORM ... ON COMMIT. The sections below cover the first and the third.

Two things the SAP LUW does not do. It does not protect against another user changing the same data meanwhile; that is the job of the SAP lock from Lock Objects and the SAP Lock Concept, which lasts for the whole SAP LUW. And it does not replace the database LUW: the registered changes still run in a database LUW, just one that is controlled.

Where an SAP LUW starts and ends

Each time a new internal session is opened, except by CALL DIALOG, a new SAP LUW starts. The statements COMMIT WORK and ROLLBACK WORK set its limits, and the end of the program always ends the last SAP LUW. One program can therefore contain several SAP LUWs one after the other. A program that is started with SUBMIT ... AND RETURN or CALL TRANSACTION opens a new SAP LUW but not a new database LUW, so a database rollback in the called program can also remove the registrations that the interrupted program had made; the documentation advises an explicit database commit before such a call. Ending a program, or closing an internal session with SUBMIT without AND RETURN or LEAVE TO TRANSACTION, while procedures are still registered, ends the SAP LUW without running them. The registered update modules cannot be run any more. The system class CL_SYSTEM_TRANSACTION_STATE has methods that tell a program the state of the current SAP LUW.

Update function modules

An update function module is a function module whose processing type is "update module". It holds the statements that change the database. A program never calls it directly. It registers it with CALL FUNCTION ... IN UPDATE TASK, which stores the name of the module and the values of the parameters in the update tables of the system (the Keyword Documentation names VBMOD and VBDATA), and nothing else happens yet. The change runs later, when the program says COMMIT WORK. A module that is registered twice is executed twice, with the parameters of each registration.

Registration has rules worth knowing, all from the Keyword Documentation:

  • The parameters are stored, so the EXPORTING list may not pass reference variables or structures that contain references, and the TABLES addition passes internal tables. IMPORTING, CHANGING and EXCEPTIONS may be written but are ignored, and sy-subrc is undefined after the registration.

  • A function module is either an update module or remote-enabled, never both. The update task builds SAP LUWs inside one AS ABAP; remote calls build units of work across systems.

  • Without COMMIT WORK nothing runs. ROLLBACK WORK deletes all registrations of the current SAP LUW.

V1 and V2

When you create an update module you choose its priority. The ABAP Development Tools call this the update task kind, and it has three values.

Update task kind in ADT Name in the platform documentation What it means

Start Immediate

V1 (the Keyword Documentation says VB1)

High priority. Runs in the SAP LUW’s update under the locks of the transaction, and the update task can restart it after an error.

Immediate Start No Restart

V1

High priority, but the update task must not restart it.

Start Delayed

V2 (VB2)

Low priority. Runs in its own update transaction after all V1 modules are finished, and can be restarted after an error.

The platform documentation gives the reason for the two levels. V1 modules carry the critical, primary changes, those that affect objects with a controlling function, for example the creation of orders or a change of material stock. V2 modules carry less critical secondary changes, for example pure statistics. The V1 modules of an update run one after the other in one update work process, in one database LUW, so they can be rolled back together, and they run under the SAP locks that the transaction set. The V2 modules run together in a separate LUW and are not covered by those locks. If the system has no work process for V2 updates, a V1 process runs them. All V1 modules must finish before the V2 modules start, and the Keyword Documentation adds that V2 modules start only when all V1 modules were completed successfully.

Example: approve a claim with two update modules

The scenario: the program Z_EXP_APPROVE approves claim C-0001. The status goes to A and the total to a new amount. The two changes are placed in two update modules, so that they are written together or not at all. The listings use the full table ZEXP_CLAIM of Database Tables and Structures and the lock object EZEXP_CLAIM of Lock Objects and the SAP Lock Concept. The two function modules are created in Function Builder or ADT, in a function group, with the processing type Update and the update task kind Start Immediate; each has the two import parameters named in the code. The bodies need an SAP system; the listing is not run here.

FUNCTION z_exp_set_claim_status.
  " import parameters: claim_id TYPE zexp_claim-claim_id, status TYPE zexp_claim-status
  UPDATE zexp_claim SET status = @status WHERE claim_id = @claim_id.
ENDFUNCTION.

FUNCTION z_exp_set_claim_total.
  " import parameters: claim_id TYPE zexp_claim-claim_id, total_amount TYPE zexp_claim-total_amount
  UPDATE zexp_claim SET total_amount = @total_amount WHERE claim_id = @claim_id.
ENDFUNCTION.

The two modules contain no COMMIT WORK and no ROLLBACK WORK: both are forbidden inside an update and end in a runtime error. The program sets the lock, registers the modules and commits. It requires an SAP system; not run here.

DATA claim_id TYPE zexp_claim-claim_id VALUE 'C-0001'.

CALL FUNCTION 'ENQUEUE_EZEXP_CLAIM'
  EXPORTING
    mode_zexp_claim = 'E'
    claim_id        = claim_id
    _scope          = '2'
  EXCEPTIONS
    foreign_lock    = 1
    OTHERS          = 2.
IF sy-subrc <> 0.
  MESSAGE |Claim { claim_id } cannot be locked.| TYPE 'I'.
  RETURN.
ENDIF.

CALL FUNCTION 'Z_EXP_SET_CLAIM_STATUS' IN UPDATE TASK
  EXPORTING
    claim_id = claim_id
    status   = 'A'.

CALL FUNCTION 'Z_EXP_SET_CLAIM_TOTAL' IN UPDATE TASK
  EXPORTING
    claim_id     = claim_id
    total_amount = 250.

COMMIT WORK AND WAIT.
IF sy-subrc <> 0.
  MESSAGE |The update of claim { claim_id } failed.| TYPE 'E'.
ENDIF.

Reading it: _scope 2 gives the lock to the update, so COMMIT WORK hands it to the update task, which removes it when the update is finished; no DEQUEUE is needed, and a ROLLBACK WORK before the commit removes the lock. In the lock page’s words the lock "can outlive the program that set it", which is exactly the aim: the data stays protected until the update has really written it. The two CALL FUNCTION statements write nothing. At COMMIT WORK AND WAIT the program stops until the update work process has executed both V1 modules in one database LUW. If it succeeded, sy-subrc is 0, and 4 if not. The sequence is drawn below.

sequenceDiagram participant U as User participant D as Dialog work process (program) participant L as Enqueue server participant T as Update tables participant W as Update work process participant B as Database (application tables) U->>D: Screens: user enters the new status and total D->>L: ENQUEUE_EZEXP_CLAIM, _SCOPE 2 L-->>D: lock set D->>T: register Z_EXP_SET_CLAIM_STATUS (IN UPDATE TASK) D->>T: register Z_EXP_SET_CLAIM_TOTAL (IN UPDATE TASK) Note over D,T: Nothing has changed in ZEXP_CLAIM yet D->>W: COMMIT WORK AND WAIT: start the update W->>T: read the registered modules and their parameters W->>B: run both V1 modules in one database LUW W->>B: database commit W->>L: release the lock held for the update W-->>D: sy-subrc 0, the program continues

COMMIT WORK and ROLLBACK WORK

COMMIT WORK closes the current SAP LUW and opens a new one. According to the Keyword Documentation it does these things, in this order:

  1. It runs the subroutines registered with PERFORM ... ON COMMIT in the current work process.

  2. It raises an internal event for the persistence service of Object Services, whose handlers can register one more update module with the changes of the persistent objects (see Object Services and Persistence).

  3. It starts the update: all high-priority (V1) modules in the order of registration and in one database LUW, then, only if they succeeded, the low-priority (V2) modules in a second database LUW.

  4. It runs the calls registered with CALL FUNCTION ... IN BACKGROUND UNIT, and with the obsolete IN BACKGROUND TASK, one database LUW per RFC destination.

  5. It handles the SAP locks of the program according to the _SCOPE parameter that set them.

  6. It triggers a database commit on all open database connections, which ends the current database LUW and closes every database cursor.

  7. It raises the event TRANSACTION_FINISHED of the system class CL_SYSTEM_TRANSACTION_STATE, with the parameter KIND set to the constant for COMMIT WORK.

The closed cursors have a practical consequence: an ABAP SQL SELECT loop that is still open when COMMIT WORK runs cannot be continued; the next attempt raises an uncatchable exception.

Synchronous, asynchronous and local. Without AND WAIT, the update is asynchronous: the program continues at once, and sy-subrc is always 0. The update may even have failed when the user sees the next screen. With AND WAIT, the program waits until the V1 modules have run (synchronous update) and sy-subrc tells the result: 0 for success, 4 for failure. The V2 modules are not awaited. The third way is the local update, switched on with SET UPDATE TASK LOCAL before the first registration of the SAP LUW. The V1 modules are then kept in ABAP memory instead of the update tables and run immediately in the current work process, in the same database LUW, but in a separate internal session. The update is always synchronous then, whatever COMMIT WORK says. The statement sets sy-subrc to 0, or to 1 when an update module was already registered the normal way in this SAP LUW. The local update is switched off at the beginning of every SAP LUW and does not affect V2 modules. A database rollback in a local update affects all previous change requests. The Keyword Documentation also names the profile parameter abap/force_local_update_task, which switches the local update on for a whole system, and warns to use it with great care.

ROLLBACK WORK closes the SAP LUW the other way. It runs the subroutines registered with PERFORM ... ON ROLLBACK, discards the registered ON COMMIT subroutines, deletes the registered update modules and background RFC calls from their tables, removes the SAP locks that were set with _SCOPE 2, rolls back all open database connections and raises TRANSACTION_FINISHED with the rollback constant. After it, sy-subrc is always 0, so querying it is pointless. It must not be used during an update or in a subroutine registered for the commit. A type A message that is caught by the predefined exception error_message of a function module call also executes ROLLBACK WORK implicitly.

Neither statement may be used in an update, in a subroutine called at the commit, or in units managed by background RFC; the runtime errors are named COMMIT_IN_POSTING, ROLLBACK_IN_POSTING, COMMIT_IN_PERFORM_ON_COMMIT and ROLLBACK_IN_PERFORM_ON_COMMIT.

A commit and a rollback that run in the lab

The local ABAP playground of this site (the open-abap lab, with the reduced version of ZEXP_CLAIM that has only claim_id, status and amount) can show the effect of the two statements on the database. It has no update task, no lock server and no work processes, so it cannot show registration or the update. The report inserts a row and rolls back, inserts it again and commits, then rolls back once more and counts the rows each time. This listing was run in the lab.

REPORT zp_luw.

DATA count TYPE i.

INSERT zexp_claim FROM @( VALUE #( claim_id = 'C-0001' status = 'N' amount = 100 ) ).
ROLLBACK WORK.
SELECT COUNT(*) FROM zexp_claim INTO @count.
WRITE / |after ROLLBACK WORK: rows { count }|.

INSERT zexp_claim FROM @( VALUE #( claim_id = 'C-0001' status = 'N' amount = 100 ) ).
COMMIT WORK.
WRITE / |COMMIT WORK: sy-subrc { sy-subrc }|.
ROLLBACK WORK.
SELECT COUNT(*) FROM zexp_claim INTO @count.
WRITE / |after COMMIT WORK and a later ROLLBACK WORK: rows { count }|.

Output:

after ROLLBACK WORK: rows 0
COMMIT WORK: sy-subrc 0
after COMMIT WORK and a later ROLLBACK WORK: rows 1

Reading it: the row inserted before the first ROLLBACK WORK is gone, the row inserted before COMMIT WORK survives the later ROLLBACK WORK, because a rollback only undoes what happened since the last commit. The statement PERFORM ... ON COMMIT is deliberately left out of the lab listing: the playground ran the registered subroutine at once instead of at the commit, so it would show the wrong behavior.

Subroutines registered for the commit

The third bundling technique registers a subroutine, an old kind of procedure written between FORM and ENDFORM, with PERFORM subr ON COMMIT or PERFORM subr ON ROLLBACK. Nothing runs at registration. COMMIT WORK or ROLLBACK WORK then runs the registered subroutines in the current work process, before any update function module is called. With COMMIT, the addition LEVEL idx (an integer, 0 by default) sorts the execution by ascending value; equal values run in order of registration. A subroutine registered several times runs once per registration.

The Keyword Documentation limits this technique. A registered subroutine has no parameter interface, so data reaches it only through external means such as ABAP memory, and the documentation concludes that such subroutines suit management tasks, for example cleanup at the end of an SAP LUW, better than database changes. Creating subroutines is obsolete: if new ones are needed for this, they should only wrap a method call. During the run of a registered subroutine, a further registration, COMMIT WORK and ROLLBACK WORK are forbidden, but explicit database commits and rollbacks are allowed. The sketch below registers a cleanup. It requires an SAP system; not run here.

PERFORM release_buffer ON COMMIT.
PERFORM release_buffer ON ROLLBACK.

COMMIT WORK.

FORM release_buffer.
  " a wrapper: call the method that clears the buffers of this SAP LUW
ENDFORM.

One trap deserves a plain statement. The subroutines run in the current work process, in a different database LUW from the update modules. With an asynchronous update, the changes of the subroutine can be committed before the update modules run, and a database rollback in the update does not undo them. The documentation therefore asks for special care whenever both techniques are combined, and for database changes the update module is the right tool.

Errors in the update

The program does not see what the update work process does after an asynchronous COMMIT WORK. If a runtime error happens in an update module, the update work process performs a database rollback, writes the failure to the update tables and informs the user whose program created the entries, through SAPMail. After the cause is fixed, the cancelled entries can be updated again. Update management is transaction SM13, which lets you display update requests, analyze problems, test and clean up cancelled requests, reset their status and delete them. A cancelled update record has the status Error; the platform documentation describes how to open the error text or the short dump of the failing module, and how to look in the system log (transaction SM21) for the message "Update was aborted". The authorization object S_ADMI_FCD with the values UADM, UMON and UDSP decides who may administer, manage or only display updates; the administration of the update system itself uses transaction SM14. Both are SAP GUI tools of classic systems.

Restrictions apply while an update runs, all of which cause runtime errors if broken: no database commit and no database rollback, no change of the update control, and no authorization check. So an update module must contain only the database change itself; any check belongs before the registration, in the dialog part. A subroutine registered with PERFORM ... ON COMMIT may be registered during an update and then runs at the end of that update.

When a program ends, or an internal session closes, with registrations and no COMMIT WORK, the modules never run. The documentation of COMMIT WORK says the entries stay on the database but can no longer be executed, while the documentation of the registration statement says they are deleted at the end of the program. Either way, the change does not happen; the safe habit is to treat an SAP LUW without a commit as abandoned.

The controlled SAP LUW

In classic ABAP, obeying the SAP LUW was left to the developer, and nothing checked it. The platform documentation says it is fairly easy to break the rules without meaning to. The controlled SAP LUW adds the missing check: it detects invalid statements, and calls of APIs that contain them, in the two transactional phases. The modify phase is the time in which data is changed in a buffer, with the user still able to correct entries. The save phase is the final step in which the consistent data is written to the database. A one-shot web call has both in one request; a UI scenario has user interaction between them. The point of the phases is that a change must not reach the database early, because an early write can trigger workflows or events for data that is later dropped.

The documentation lists these restrictions, which the controlled SAP LUW checks:

  • No explicit database commit on the standard connection in the modify phase. Implicit commits are allowed as long as they trigger no database modification, and commits on secondary connections are allowed in every phase.

  • Database modifications only in the save phase; calling update task function modules in classic ABAP is allowed only there.

  • Synchronous and asynchronous RFC calls only in the modify phase; background RFC and calls of the background Processing Framework only in the save phase.

  • No statements that raise exceptions explicitly, and none of the classic statements for dynpros, reports or leaving the transaction.

  • Classified APIs only in the phases where their classification allows them.

In the save phase the inverse holds for commits: database updates are allowed, but implicit database commits are not. That is why the documentation says an HTTP call made during the save phase violates the logical unit of work.

Setting the phases. A program can switch the checks on by setting the phase with the static methods of the class CL_ABAP_TX. The sketch adapts the documentation’s example to the claim table. It requires an SAP system; not run here.

cl_abap_tx=>modify( ).
" here, a database change such as the UPDATE below would be a violation

cl_abap_tx=>save( ).
UPDATE zexp_claim SET status = 'A' WHERE claim_id = 'C-0001'.

Classified APIs. An API (interface, class, or function module) can carry a classification, written as a type from an interface whose name starts with IF_ABAP_TX_. It says in which phase the API may be called and what its implementation may do. The platform documentation defines six.

Classification Meaning

IF_ABAP_TX_MODIFY

May be called only in the modify phase. The implementation must not change the database or call APIs classified for the save phase.

IF_ABAP_TX_SAVE

May be called only in the save phase. The implementation must not trigger implicit database commits.

IF_ABAP_TX_FUNCTIONAL

A pure function: only ABAP SQL SELECT and other functional APIs are allowed in the implementation.

IF_ABAP_TX_READ

May be called in the modify phase; the implementation may read but not modify.

IF_ABAP_TX_SURFACE

May not be called from RAP business object implementations or from APIs with the other classifications.

IF_ABAP_TX_COMPLETE

Placed under transactional control when called: it starts in the modify phase and may move to the save phase.

Where the checks apply. Once a phase is set, a violation is an unconditional runtime error when the code uses the restricted ABAP language version (ABAP Cloud, or an artifact in classic ABAP assigned the language version ABAP for Cloud Development), and in RAP when the behavior definition is written with strict(2). In RAP, the background Processing Framework and RAP business events the checks run automatically, and in RAP without strict(2) violations are logged. In every other case, which includes ordinary classic programs, the checks run only when the checkpoint group CC_STMT is activated, and the documentation stresses that this is not the default. A page for classic maintenance should therefore treat the controlled SAP LUW as a tool to switch on while testing, not as something that protects an old program by itself.

ABAP Cloud and RAP

In ABAP Cloud the language scope is restricted, and the bundling techniques above are not available: the platform documentation says the classic update task is not available any more. RAP is the transactional programming model. It follows the rules of the SAP LUW and implements the two phases itself: the interaction phase and the early save phase form the modify phase, in which changes go to a transactional buffer, and the late save phase is the save phase, in which the consistent data is written to the database with ABAP SQL (in classic ABAP the late save may also use the update task). Reaching the late save phase is a point of no return. The statements that end the SAP LUW in RAP are COMMIT ENTITIES, which implicitly triggers COMMIT WORK and can return information to the consumer, and ROLLBACK ENTITIES, which implicitly triggers ROLLBACK WORK. For the statement COMMIT WORK itself, the Keyword Documentation for ABAP Cloud says that local updates are always enforced implicitly, so COMMIT WORK and COMMIT WORK AND WAIT behave the same; sy-subrc is normally 0 and any other value would mean a runtime error. The page ABAP SQL: Writing Data shows the matching RAP rules for table writes, and EML shows COMMIT ENTITIES. How RAP builds its business objects is the subject of the RAP pages that follow in this section.

Classic Standard ABAP

ABAP Cloud

How changes are collected

Update function modules, subroutines for the commit, background RFC

The RAP transactional buffer; update task not available

Statement that ends the unit of work

COMMIT WORK [AND WAIT] or ROLLBACK WORK

COMMIT ENTITIES or ROLLBACK ENTITIES in RAP

Who checks the rules

Nobody by default; the controlled SAP LUW when CC_STMT is on

The controlled SAP LUW, always

What older books show, and what has changed

A book written for AS ABAP 7.5 or older, such as the first edition (2016) of Complete ABAP, whose release range is the one stated on Interfaces and Inheritance, or the 2002 ABAP Objects book, comes before ABAP Cloud and RAP. Read it for the classic statements, which have not changed in meaning. Where it describes the update task as the way to write business data, keep three things in mind. First, in ABAP Cloud the update task is not available and RAP does the work. Second, the controlled SAP LUW is a check that a classic program does not get by default: it applies only when the checkpoint group CC_STMT is activated, so a book that treats the SAP LUW as a rule you simply follow does not tell you how to have it checked. Third, in ABAP Cloud COMMIT WORK and COMMIT WORK AND WAIT are the same, while the older distinction between asynchronous and synchronous update still holds in classic ABAP.

Source: SAP PRESS: Complete ABAP (the publisher page lists the current edition; the 1st edition is out of print), InformIT: ABAP Objects (Keller and Krueger, 2002) and LUWs in ABAP

Summary

  • A database LUW is the database’s all-or-nothing unit; a classic dialog program spans several of them, because every switch of work process commits implicitly. The SAP LUW is the business-level unit that must be written together.

  • Inside an SAP LUW, changes are registered and executed later in one go: as update function modules registered with CALL FUNCTION ... IN UPDATE TASK, or, only for housekeeping, as subroutines registered with PERFORM ... ON COMMIT.

  • V1 modules are the critical changes and run first, in one database LUW under the transaction’s locks; V2 modules are less critical and run afterwards in their own LUW.

  • COMMIT WORK starts the update; with AND WAIT the program waits and reads sy-subrc (0 or 4); SET UPDATE TASK LOCAL runs it in the current work process. ROLLBACK WORK discards everything registered. Neither may be used inside an update.

  • Update errors are found and repaired in SM13; no commit, rollback or authorization check is possible inside an update.

  • The controlled SAP LUW checks the modify and save phases; it is always active in ABAP Cloud and RAP, and in classic ABAP only when the checkpoint group CC_STMT is on. In ABAP Cloud the update task is not available and RAP ends the SAP LUW with COMMIT ENTITIES.