Class EntityManagerAwareRunnable

java.lang.Object
org.broadleafcommerce.common.util.EntityManagerAwareRunnable
All Implemented Interfaces:
Runnable

public abstract class EntityManagerAwareRunnable extends Object implements Runnable
Provides a Runnable implementation that allows the execution of the run() method inside the context of a thread-bound EntityManager.

This MUST be run in a background thread (typically a TaskExecutor; not a HTTP request thread, a EntityManagerFactoryAwareSystemEventConsumer, or a thread to which an EntityManager has already been bound).

Author:
Kelly Tisdell
  • Field Details

  • Constructor Details

    • EntityManagerAwareRunnable

      public EntityManagerAwareRunnable()
      Constructs an abstract Runnable implementation that can be run in another thread. Guarantees that an EntityManager is bound to the current thread.
    • EntityManagerAwareRunnable

      public EntityManagerAwareRunnable(Semaphore sem)
      Constructs an abstract Runnable implementation that can be run in another thread. Guarantees that an EntityManager is bound to the current thread.

      If a Semaphore is provided (not null), a single permit will be released prior to the completion of the run method.

      Parameters:
      sem -
  • Method Details

    • run

      public final void run()
      This method attempts to create / bind an EntityManager prior to execution. This will also create and bind a BroadleafRequestContext, which should not already be bound to the background thread. If the run method is executed in a foreground thread, any existing BroadleafRequestContext bound to the thread will be returned to its previous state.
      Specified by:
      run in interface Runnable
    • executeInternal

      protected abstract void executeInternal() throws Exception
      Executes within the context of a thread-bound EntityManager. Helps avoid LazyInitializationExceptions and exceptions indicating that the Hibernate Session is closed.
      Throws:
      Exception
    • getEntityManagerName

      public String getEntityManagerName()
      The name of the persistence unit.

      Defaults to "blPU", the main persistence unit in Broadleaf.

      Returns:
    • getEntityManagerFactory

      protected jakarta.persistence.EntityManagerFactory getEntityManagerFactory()
    • getApplicationContext

      protected org.springframework.context.ApplicationContext getApplicationContext()
    • getEntityManager

      protected final jakarta.persistence.EntityManager getEntityManager()
    • registerError

      protected void registerError(Exception e)