Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Object Services →  transaction service 

Nesting and Chaining Transactions

Other versions: 7.31 | 7.40 | 7.54

Role of the Top Level Transaction

The top level transaction is the transaction of the uppermost level. All the transactions started during the top level transaction are subtransactions and do not lead to a COMMIT WORK when they end. The transaction mode determines how the top level transaction is created.

  • Compatibility Mode
The top level transaction is usually created implicitly when the Object Services are initialized. All explicit transactions of the program are subtransactions of this implicit top level transaction.
  • Object-Oriented Transaction Mode
The top level transaction is created explicitly when the first transaction in the program is started. All the other transactions that are started before the top level transaction ends are subtransactions of the explicit top level transaction.

Subtransactions

You can create a subtransaction by starting a new transaction before another transaction has come to an end. Subtransactions must be ended within the transaction in which they are embedded. A COMMIT WORK is never triggered when a subtransaction ends. Changes to the persistent objects of a subtransaction are only made when the top level transaction has ended. This occurs explicitly in a COMMIT WORK or implicitly in an END, depending on the transaction mode. You can also query the transaction mode with IF_OS_TRANSACTION~GET_MODES for subtransactions, but the transaction mode always tells you the global setting of the Object Services for the current program.

Sequential Processing

Top Level Transactions

A program can only have one top level transaction at a time. A new transaction can be started as the top level transaction when the old top level transaction has come to an end.

  • Compatibility Mode
In compatibility mode, a new top level transaction is implicitly created and started when the old top level transaction has ended with COMMIT WORK. The new top level transaction manages the changes made to persistent objects.
  • Object-Oriented Mode
When the top level transaction has ended with END, a COMMIT WORK is triggered. The transaction object is invalidated in the program and cannot be started again. The next transaction that is started with IF_OS_TRANSACTION~START becomes the new top level transaction. A program is without a transaction between the end of the last and the start of the next top level transaction. Changes made to persistent objects before the new transaction was started are ignored.

Within a transaction, further subtransactions can be started after the end of another subtransaction. Changes made to persistent objects between subtransactions belong to the transaction at the next higher level.

Concatenation of Top Level Transactions

If you execute transactions one after the other, that is you end one top level transaction and then start a new one, the relevant persistent objects are invalidated. When you access such an object in the next transaction, it is loaded again from the database. You can avoid this time-consuming process by concatenating top level transactions with IF_OS_TRANSACTION~END_AND_CHAIN or IF_OS_TRANSACTION~UNDO_AND_CHAIN. In this case, the persistent objects are not invalidated. However, a new transaction object is created and its reference is returned as the return value.

You can use these methods for top level transactions in object-oriented mode, but not for the top level transaction in compatibility mode. You do not need to use the methods for subtransactions since the persistent objects are retained until the change has been made, and are not invalidated.


Example

After method END_AND_CHAIN is called, reference variable t refers to a new transaction object.

DATA tm TYPE REF TO if_os_transaction_manager.
DATA t  TYPE REF TO if_os_transaction.
...
tm = cl_os_system=>get_transaction_manager( ).
t = tm->create_transaction( ). "Create first Transaction
t->start( ).                   "Start first Transaction
...
t = t->end_and_chain( ).       "End first Transaction,
                               "create and start second one
...
t->end( ).                     "End second Transaction