company logo

Transactions in context handlers

When providing a context handler function, transactions need not and should not be started. Since the transaction handler is called while an ODABA function is running, the ODABA function has already started a read transaction and, when necessary, an update transaction, as well.

When, however, trying to make changes in a read request, an update transaction might be started also in a read handler function. In order to determine, whether a starting a transaction id possible or not, one may call internalTransaction() . When this property is true, starting an update transaction will throw an exception.

... fragment doAfterRead ( ) {

  ObjectSpace    os(objectSpaceContext().objectSpace());

  int            taLevel = 0;

  

  if ( !os.internalTransaction() )

    taLevel = os.beginTransaction();

    

  // ... do updates

  

  if ( taLevel > 0 ) // transaction has been started

    os.commitTransaction(taLevel);

}