Class EntityManagerExecutor

java.lang.Object
org.axonframework.common.jpa.EntityManagerExecutor
All Implemented Interfaces:
MaterializationAware, TransactionalExecutor<jakarta.persistence.EntityManager>

public class EntityManagerExecutor extends Object implements TransactionalExecutor<jakarta.persistence.EntityManager>, MaterializationAware
A TransactionalExecutor implementation for EntityManagers.

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

This executor never commits, rolls back, or closes the entity manager itself; that remains the responsibility of whoever supplied it through EntityManagerProvider. This is deliberate: an entity manager reached through this class is typically an ambient one bound to an externally-managed unit of work, 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<jakarta.persistence.EntityManager,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<jakarta.persistence.EntityManager>
      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