Interface TransactionalExecutor<T>

Type Parameters:
T - the type of the resource
All Known Implementing Classes:
ConnectionExecutor, EntityManagerExecutor

public interface TransactionalExecutor<T>
Executes transactional operations with automatic transaction management. The user of this interface will get (limited) access to a transactional resource, like a JDBC connection or an Entity Manager, with which to perform operations.

At the appropriate time in the lifecycle of this executor, commit or rollback is called on the transactional resource managed by it. Any (uncaught) exceptions will result in the resource to be rolled back, and any associated lifecycle to be put into an error state.

Whether "the appropriate time" is driven by this executor itself or by an external owner of the resource depends on the implementation: an executor managing an independently-obtained resource typically commits, rolls back, and closes it as part of apply(org.axonframework.common.function.ThrowingFunction<T, R, java.lang.Exception>)/accept(org.axonframework.common.function.ThrowingConsumer<T, java.lang.Exception>) completing, while one wrapping an ambient resource bound to an externally-managed unit of work (e.g. ConnectionExecutor wrapping a container's transaction-bound connection) leaves that entirely to the external owner instead.

This interface provides convenience methods for both operations that return a result and operations that are purely side-effecting.

Since:
5.0.2
Author:
John Hendrikx
  • Method Details

    • accept

      default CompletableFuture<Void> accept(ThrowingConsumer<T,Exception> consumer)
      Executes a transactional operation that does not return a result.

      Implementations are responsible for managing the lifecycle of the provided resource, including obtaining, closing, commit and rollback.

      Parameters:
      consumer - a consumer which accepts the transactional resource of type T; cannot be null
      Returns:
      a CompletableFuture with a void result, never null
      Throws:
      NullPointerException - when consumer is null
    • apply

      <R> CompletableFuture<R> apply(ThrowingFunction<T,R,Exception> function)
      Executes a transactional operation that returns a result.

      Implementations are responsible for managing the lifecycle of the provided resource, including obtaining, closing, commit and rollback.

      Type Parameters:
      R - the type of the result returned by the function
      Parameters:
      function - a function that accepts the transactional resource of type T and produces a result of type R; cannot be null
      Returns:
      a CompletableFuture which when it completes contains the result of the provided function, never null
      Throws:
      NullPointerException - when function is null