(⬆️ back to top) ## SAP LUW Overview For an [SAP LUW](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abensap_luw_glosry.htm), the following aspects come into play: - Usually, an SAP LUW is started by opening a new [internal session](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abeninternal_session_glosry.htm). The execution of programming units can be distributed among several work processes. - Database commits persist data in the database (especially implicit database commits when work processes are switched). - The all-or-nothing rule applies: All database changes that occur during a transaction constitute a logical unit of work. They must be committed together, or rolled back together in the event of an error. - This means that all database changes must be deferred and made in a final database commit (that is, in a single database LUW) at the end of a transaction - not in between. - To ensure data consistency, all database changes are bundled, for example, in temporary tables or transactional buffers, and then, at the end of the transaction, all changes are written to the database together in a single work process, that is, in a single database LUW with a single database commit. This can be done directly using ABAP SQL statements at this stage, or using bundling techniques - special ABAP programming techniques. Using the above bank transfer as an example: - At the end of the transaction, the new totals of both accounts are updated (money is debited from account A and credited to B). - You cannot debit account A in one work process and then credit account B in a separate work process. When the work process changes, new totals would be available in one account, but not in the other. Consider the consequences if an error occurs and the new totals for account B cannot be updated, and so on. You would no longer be able to easily roll back the changes. - Consider prematurely updating the database and notifying the users or processing the data while the logical unit has not been successfully completed.
(⬆️ back to top)
### Bundling Techniques
The following bundling techniques are available for classic ABAP. This means that programming units are registered in different work processes, but are executed by a single work process. All database changes are put into one database LUW, and all changes are committed in one final database commit.
**Using [update function modules](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenupdate_function_module_glosry.htm)**
- Are specially marked, i.e. the *update module* property is marked
- Can be given specific attributes to determine the priority with which they are processed in the update work process
- Usually contain database modification operations/statements
- [CALL FUNCTION ... IN UPDATE TASK](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapcall_function_update.htm) statements are used to register the update function modules for later execution; the actual execution is triggered by a `COMMIT WORK` statement
- Example of a simple update function module that has an importing parameter (a structure that is used to modify a database table)
```abap
FUNCTION zsome_update_fu_mod
IMPORTING
VALUE(values) TYPE some_dbtab.
MODIFY some_dbtab FROM values.
ENDFUNCTION.
```
- Depending on your use case, you can run the update work process in several ways:
- [Synchronous update](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abensynchronous_update_glosry.htm): The calling program waits until the update work process has finished. The `COMMIT WORK` statement with the `AND WAIT` addition triggers a synchronous update in a separate update work process.
- [Asynchronous update](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenasynchronous_update_glosry.htm): The calling program does not wait for the update work process to finish. The `COMMIT WORK` statement triggers an asynchronous update in a separate update work process.
- [Local update](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenlocal_update_glosry.htm): The update is performed immediately in the current work process in a separate internal session and not in a separate update work process, so that a synchronous update is always performed (regardless of whether `COMMIT WORK` is used with `AND WAIT` or not). By default, the local update is deactivated at the start of each SAP LUW. If required, you can activate local update for an SAP LUW using the [`SET UPDATE TASK LOCAL`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapset_update_task_local.htm) statement before registering the update function modules.
```abap
"Synchronous update
DATA(st_a) = VALUE some_type( ... ).
...
"Registering the update function module specified in uppercase letters
CALL FUNCTION 'ZSOME_UPDATE_FU_MOD' IN UPDATE TASK
EXPORTING values = st_a.
...
"Triggering the synchronous update
COMMIT WORK AND WAIT.
***********************************************************************
"Asynchronous update
DATA(st_b) = VALUE some_type( ... ).
...
CALL FUNCTION 'ZSOME_UPDATE_FU_MOD' IN UPDATE TASK
EXPORTING values = st_b.
...
"Triggering the asynchronous update
COMMIT WORK.
***********************************************************************
"Local update
"Before update function modules are registered.
SET UPDATE TASK LOCAL.
DATA(st_c) = VALUE some_type( ... ).
...
CALL FUNCTION 'ZSOME_UPDATE_FU_MOD' IN UPDATE TASK
EXPORTING values = st_c.
...
"The update will be synchronous.
COMMIT WORK.
```
> **💡 Note**
> If a runtime error occurs during the update, the update work process executes a database rollback, and notifies the user whose program created the entries.
**Using [remote-enabled function modules](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenremote_enabled_fm_glosry.htm)**
- Also in this case, the bundling is done through function modules.
- They are also specially marked as remote-enabled function modules.
- For example, you can use register them for later asynchronous execution in the background and through the [RFC interface](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenrfc_interface_glosry.htm) ([background Remote Function Call (bgRFC)](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenbg_remote_function_glosry.htm)). With this technology, you can make calls in the same or different ABAP systems.
- More information:
- [SAP Help Portal documentation about RFC](https://help.sap.com/docs/ABAP_PLATFORM_NEW/753088fc00704d0a80e7fbd6803c8adb/4888068AD9134076E10000000A42189D)
- [`CALL FUNCTION ... IN BACKGROUND UNIT`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapcall_function_background_unit.htm)
**Using [subroutines](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abensubroutine_glosry.htm)**
- Subroutines that are no longer recommended for use can be registered for later execution.
- They are registered with the [`PERFORM ... ON COMMIT`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapperform_on_commit.htm) statement. These subroutines are executed when a `COMMIT WORK` statement is called.
- An addition is available to control the order of execution.
- Similarly, a subroutine can be registered "on rollback" with [`PERFORM ... ON ROLLBACK`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapperform_on_commit.htm). These subroutines are executed when a `ROLLBACK WORK` statement is called.
- When executed:
- In the current work process, before update function modules.
- When they are registered in an update function module with `ON COMMIT`, they are executed at the end of the update. This happens in the update work process for non-local updates, and in the current work process for local updates.
(⬆️ back to top)
### Related ABAP Statements
An SAP LUW is usually started by opening a new internal session.
The statements to end an SAP LUW have already been mentioned above: [`COMMIT WORK [AND WAIT]`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapcommit.htm) and [`ROLLBACK WORK`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abaprollback.htm).
`COMMIT WORK [AND WAIT]`
- Closes the current SAP LUW and opens a new one.
- Commits all change requests in the current SAP LUW.
- Among other things, this statement triggers ...
- the processing of all registered update function modules.
- the update work process and the local updates in the current work process.
- a database commit for all currently open [database connections](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abendatabase_connection_glosry.htm), which also terminates the current database LUW.
- Note that `COMMIT WORK` triggers an asynchronous, `COMMIT WORK AND WAIT` a synchronous update.
`ROLLBACK WORK`
- Similar to `COMMIT WORK` statements, this statement closes the current SAP LUW and opens a new one.
- Among other things, this statement ...
- causes all changes within a SAP LUW to be undone, that is, all previous registrations for the current SAP LUW are removed.
- triggers a database rollback on all currently open database connections, which also terminates the current database LUW.
> **💡 Note**
> Notes on database connections:
> - The [database interface](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abendatabase_interface_glosry.htm) uses the [standard connection](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenstandard_db_connection_glosry.htm) of the current work process to access the [standard database](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenstandard_db_glosry.htm) by default.
> - Optionally, database accesses can also be made by using [secondary connections](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abensecondary_db_connection_glosry.htm) to [secondary databases](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abensecondary_db_glosry.htm) or by using [service connections](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenservice_connection_glosry.htm) to the standard database. The secondary connections are usually used by technical components. For example, they are used for caches, traces, logs, and so on.
> - The implicit database rollback is performed on all database connections that are currently open.
> - Within the SAP LUW, database changes and commits are allowed on service connections or through secondary database connections.
(⬆️ back to top)
## Concepts Related to the SAP LUW
The following concepts are related to the SAP LUW to ensure transactional consistency. They are not discussed in detail here. For more information, see the links.
**Authorization concept**
- In an SAP system, you need to protect data from unauthorized access by making sure that only those [authorized](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenauthorization_glosry.htm) to access it can see and modify it.
- Authorization to access data can be set. Before a user can perform certain operations in your application, you need to implement authorization checks.
- Since ABAP SQL statements do not trigger any [authorization checks](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenauthorization_check_glosry.htm) in the database system, this is even more important. Database tables may be accessed without restriction using these statements. Conversely, not all users in a system are authorized to access all data available to ABAP SQL statements.
- Thus, it is up to the programmer to ensure that each user who can call the program is authorized to access the data it handles.
- More information:
- [Authorizations](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenbc_authority_check.htm)
- [`AUTHORITY-CHECK`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapauthority-check.htm)
**Lock concept**
- The database system automatically sets [database locks](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abendatabase_lock_glosry.htm) when ABAP SQL statements are called to modify database table entries.
- These locks are implemented by automatically setting a lock flag, which can only be set for existing database table entries.
- After a database commit, these flags are removed.
- As a result, database locks are not available for more than one database LUW, which must be considered in the context of an SAP LUW, since multiple database LUWs may be involved. Therefore, the lock flags that are set in a transaction are not sufficient. For the duration of an entire SAP LUW, a lock on database entries must remain set.
- This is where the SAP lock concept comes into play, which is independent of the automatic database locks. It is based on [lock objects](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenlock_object_glosry.htm).
- Lock objects ...
- are [repository objects](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenrepository_object_glosry.htm) that are defined in the [ABAP Dictionary](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenabap_dictionary_glosry.htm).
- specify the database tables in which records are to be locked with a lock request.
- contain the key fields on which a lock is to be set.
- When a lock object is created, two [lock function modules](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenlock_function_module_glosry.htm) (`ENQUEUE_...` and `DEQUEUE_...`) are automatically generated. They are executed in a special enqueue work process. When a record is locked during a transaction (by the enqueue function module), a central lock table is filled with the table name and key field information. Unlike database locks, a locked entry in a lock object does not necessarily have to exist in a database table. Also, the locking must be done proactively, i. e. there is no automatic locking. You must make sure that the application implementation checks the lock entries.
- At the end of an SAP LUW, all locks should be released, either automatically during the database update or explicitly when you call the corresponding dequeue function module.
- More information: [SAP Locks](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abensap_lock.htm)
> **💡 Note**
> RAP comes with its own implementation features to cover these concepts. See the topics [Authorization Control](https://help.sap.com/docs/SAP_S4HANA_CLOUD/e5522a8a7b174979913c99268bc03f1a/375a8124b22948688ac1c55297868d06.html) and [Concurrency Control](https://help.sap.com/docs/SAP_S4HANA_CLOUD/e5522a8a7b174979913c99268bc03f1a/d315c13677d94a6891beb3418e3e02ed.html) in the *Development guide for the ABAP RESTful Application Programming Model*.
(⬆️ back to top) ## Notes on the SAP LUW in ABAP Cloud and RAP A limited set of ABAP language features is available in ABAP Cloud ([restricted ABAP language version](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenrestricted_version_glosry.htm)). The limitations include the fact that the above bundling techniques are not available. Note that the local update is enabled by default in ABAP Cloud. Find more information in this [blog](https://blogs.sap.com/2022/12/05/the-sap-luw-in-abap-cloud/). In fact, RAP is the transactional programming model for ABAP Cloud. And RAP comes with a well-defined transactional model and follows the rules of the SAP LUW. At the end of an SAP LUW in RAP, database modification operations should be performed in a final step in the [RAP late save phase](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenlate_rap_save_phase_glosry.htm) by persisting the consistent data in the [RAP transactional buffer](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abentransactional_buffer_glosry.htm) to the database. There are RAP-specific, [ABAP EML](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenabap_eml_glosry.htm) statements for commit and rollback: - [`COMMIT ENTITIES`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapcommit_entities.htm) implicitly triggers `COMMIT WORK`. Furthermore, `COMMIT ENTITIES` provides RAP-specific functionality with various additions. These EML statements implicitly enforce local updates with `COMMIT WORK`, or `COMMIT WORK AND WAIT` if the local update fails. Therefore, the update is either a local update or a synchronous update, but never an asynchronous update. - [`ROLLBACK ENTITIES`](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abaprollback_entities.htm): Resets all changes of the current transaction and clears the transactional buffer. The statement triggers `ROLLBACK WORK`. ## More Information - [The RAP Transactional Model and the SAP LUW](https://help.sap.com/docs/SAP_S4HANA_CLOUD/e5522a8a7b174979913c99268bc03f1a/ccda1094b0f845e28b88f9f50a68dfc4.html) (Development guide for the ABAP RESTful Application Programming Model) - [The SAP LUW in ABAP Cloud](https://blogs.sap.com/2022/12/05/the-sap-luw-in-abap-cloud/) (blog) - [Cheat sheet about ABAP EML](08_EML_ABAP_for_RAP.md)
(⬆️ back to top) ## Executable Example Coming soon ...