Accessing the transaction context
2018 June Release

Accessing the transaction contextPermanent link for this heading

The cootx variable can be used to access the current transaction context. The most important methods of a transaction are listed in chapter “Accessing the transaction context”.

In some scenarios it is necessary to carry out operations in a separate transaction. Any changes that have been made in a new transaction can be committed or rolled back separately from the main transaction.

Example

usecase CreateInvoice() {
  variant Order {
    impl = expression {
      // Create a new transaction
      interface @extension = coort.GetExtension();
      transaction @localtx = @extension.CreateTransaction();
      transaction @backuptx = cootx;

      try {
        coort.SetThreadTransaction(@localtx);

        Invoice @invoice = #Invoice.ObjectCreate();
        @invoice.APPDUCXSAMPLE_200_300_InitializeInvoice();

        @localtx.Commit();
      }
      catch (error) {
        @localtx.Abort();
      }
      finally {
        // Restore original transaction context
        coort.SetThreadTransaction(@backuptx);

        // Clear the variables holding the transactions
        @backuptx = null;
        @localtx = null;
      }
    }
  }
}

Note: A better and simpler way to create transactions is using the try new transaction statement. Please refer to chapter 14.10.5 Creating new transactions or opening a transaction scope” for further information.

Working with transaction variablesPermanent link for this heading

Syntax

// Retrieving the value stored in a transaction variable
value = #TV.reference;

// Storing a value in a transaction variable
#TV.reference = value;


The #TV object is a special object that provides access to transaction variables.

Note: Transaction variables can also be accessed using the cootx variable. Please refer to chapter “Predefined variables” for further information.

Example

// Retrieving the value stored in a transaction variable
@printinvoice = #TV.TV_PRINTINVOICE;

// Storing a value in a transaction variable
#TV.TV_INVOICE = cooobj;