TransactionalProcessor (interface)

Provides the mechanism to run a block of code within a transaction boundary, using Spring Framework’s transaction primitives (such as TransactionDefinition and its annotation equivalent, Propagation ).

API

TransactionalProcessor.java
interface TransactionalProcessor {
  Result<T> callTransactional(TransactionDefinition def, Callable<T> callable)     (1)
  Result<Void> runTransactional(TransactionDefinition def, ThrowingRunnable runnable)     (2)
  Result<T> callTransactional(Propagation propagation, Callable<T> callable)     (3)
  Result<Void> runTransactional(Propagation propagation, ThrowingRunnable runnable)     (4)
  Result<T> callWithinCurrentTransactionElseCreateNew(Callable<T> callable)     (5)
  Result<Void> runWithinCurrentTransactionElseCreateNew(ThrowingRunnable runnable)     (6)
}
1 callTransactional(TransactionDefinition, Callable)

Runs given callable with a transactional boundary, where the detailed transactional behavior is governed by given TransactionDefinition def .

2 runTransactional(TransactionDefinition, ThrowingRunnable)

Runs given runnable with a transactional boundary, where the detailed transactional behavior is governed by given TransactionDefinition def .

3 callTransactional(Propagation, Callable)

Runs given callable with a transactional boundary, where the detailed transactional behavior is governed by given Propagation propagation .

4 runTransactional(Propagation, ThrowingRunnable)

Runs given runnable with a transactional boundary, where the detailed transactional behavior is governed by given Propagation propagation .

5 callWithinCurrentTransactionElseCreateNew(Callable)

Runs given callable within an existing transactional boundary, or in the absence of such a boundary, creates a new one.

6 runWithinCurrentTransactionElseCreateNew(ThrowingRunnable)

Runs given runnable within an existing transactional boundary, or in the absence of such a boundary creates a new one.

Members

callTransactional(TransactionDefinition, Callable)

Runs given callable with a transactional boundary, where the detailed transactional behavior is governed by given TransactionDefinition def .

runTransactional(TransactionDefinition, ThrowingRunnable)

Runs given runnable with a transactional boundary, where the detailed transactional behavior is governed by given TransactionDefinition def .

callTransactional(Propagation, Callable)

Runs given callable with a transactional boundary, where the detailed transactional behavior is governed by given Propagation propagation .

More fine grained control is given via #callTransactional(TransactionDefinition, Callable)

runTransactional(Propagation, ThrowingRunnable)

Runs given runnable with a transactional boundary, where the detailed transactional behavior is governed by given Propagation propagation .

More fine grained control is given via #runTransactional(TransactionDefinition, ThrowingRunnable)

callWithinCurrentTransactionElseCreateNew(Callable)

Runs given callable within an existing transactional boundary, or in the absence of such a boundary, creates a new one.

In other words, support a current transaction, create a new one if none exists.

runWithinCurrentTransactionElseCreateNew(ThrowingRunnable)

Runs given runnable within an existing transactional boundary, or in the absence of such a boundary creates a new one.

Implementation

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

Example Usage

The following code is taken from the SimpleApp, showing how the service can be used to provide a custom REST controller:

@RestController
@RequiredArgsConstructor
class CustomController {

  private final InteractionFactory interactionFactory;
  private final TransactionalProcessor transactionalProcessor;  (1)
  private final SimpleObjects repository;

  @GetMapping("/custom/simpleObjects")
  List<SimpleObject> all() {
    return callAuthenticated(newAuthentication(), repository::listAll).presentElseFail();
  }

  private SimpleAuthentication newAuthentication() {
    return SimpleAuthentication.validOf(UserMemento.ofName("sven"));
  }

  private <T> Result<T> callAuthenticated(
          final Authentication authentication,
          final Callable<T> task) {
    return interactionFactory.callAuthenticated(
            authentication,
            () -> transactionService.callWithinCurrentTransactionElseCreateNew(task));
  }
}

See also

  • InteractionFactory

    Not formal API, but used in the example above to obtain an outer "interaction" (session) within which the transaction is executed.

  • Result

    which captures the result of an operation, either its successful return value or an exception.