TransactionService

Provides utilities to access active transactions associated with the current thread.

This is a low-level service that domain objects will typically have little need to leverage; there will normally be a transaction started already by the framework at the beginning of an org.apache.causeway.applib.services.iactn.Interaction and committed at the end. On occasion though it can be useful to take explicit control over transaction boundaries, which is where the methods provided by this domain service can be useful.

there is no method to close (which is to say to commit) an existing transaction, because of a subtlety with the JPA transaction manager: if a transaction wasn’t a new one, then closing the transaction would actually mean to decrement the transaction counter (only when it hits zero should the transaction be committed); but if the previous transaction was instead suspended, the commit the new transaction and then resume the previous transaction.

API

TransactionService.java
interface TransactionService {
  Optional<TransactionId> currentTransactionId()     (1)
  TransactionState currentTransactionState()     (2)
  void flushTransaction()     (3)
}
1 currentTransactionId()

Optionally returns the unique identifier of the current thread’s transaction, based on whether there is an active transaction associated with the current thread.

2 currentTransactionState()

Returns the state of the current thread’s transaction., or returns TransactionState#NONE , if there is no active transaction associated with the current thread.

3 flushTransaction()

Flushes all changes to the object store.

Members

currentTransactionId()

Optionally returns the unique identifier of the current thread’s transaction, based on whether there is an active transaction associated with the current thread.

currentTransactionState()

Returns the state of the current thread’s transaction., or returns TransactionState#NONE , if there is no active transaction associated with the current thread.

flushTransaction()

Flushes all changes to the object store.

Occasionally useful to ensure that newly persisted domain objects are flushed to the database prior to a subsequent repository query.

If there is no active transaction associated with the current thread, then does nothing.

Implementation

The core framework provides a default implementation of this service, o.a.c.core.runtimeservices.transaction.TransactionServiceSpring.