Class ConnectionExecutor

java.lang.Object
org.axonframework.common.jdbc.ConnectionExecutor
All Implemented Interfaces:
MaterializationAware, TransactionalExecutor<Connection>

public class ConnectionExecutor extends Object implements TransactionalExecutor<Connection>, MaterializationAware
A TransactionalExecutor implementation for JDBC Connections.

The connection is obtained from the given ConnectionProvider at most once, the first time apply(org.axonframework.common.function.ThrowingFunction<java.sql.Connection, R, java.lang.Exception>) is called, and reused for the remainder of this instance's lifetime. Concurrent apply(org.axonframework.common.function.ThrowingFunction<java.sql.Connection, R, java.lang.Exception>) calls are serialized, since a single Connection is not safe for concurrent use.

This executor never commits, rolls back, or closes the connection itself; that remains the responsibility of whoever supplied it through ConnectionProvider. This is deliberate: a connection reached through this class is typically an ambient one bound to an externally-managed unit of work (e.g. published by a container's transaction manager), whose commit, rollback, and close lifecycle is controlled by that external owner, not this executor.

Since:
5.0.2
Author:
John Hendrikx
  • Constructor Details

  • Method Details

    • withMaterializationShielded

      public <R> R withMaterializationShielded(Function<Boolean,R> decision)
      Description copied from interface: MaterializationAware
      Runs decision, passing whether this executor already has a resource materialized, while preventing a concurrent TransactionalExecutor.apply(org.axonframework.common.function.ThrowingFunction<T, R, java.lang.Exception>) call from materializing it for the duration - so a caller can fall back to an independent executor without racing a concurrent materialization.
      Specified by:
      withMaterializationShielded in interface MaterializationAware
      Type Parameters:
      R - the type of the result produced by decision
      Parameters:
      decision - given true if this executor already has a resource materialized, otherwise false; run shielded from a concurrent materialization, cannot be null
      Returns:
      the result of decision
    • apply

      public <R> CompletableFuture<R> apply(ThrowingFunction<Connection,R,Exception> function)
      Description copied from interface: TransactionalExecutor
      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.

      Specified by:
      apply in interface TransactionalExecutor<Connection>
      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