Class EntityManagerAwareRunnable
java.lang.Object
org.broadleafcommerce.common.util.EntityManagerAwareRunnable
- All Implemented Interfaces:
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidExecutes within the context of a thread-boundEntityManager.protected org.springframework.context.ApplicationContextprotected final jakarta.persistence.EntityManagerprotected jakarta.persistence.EntityManagerFactoryThe name of the persistence unit.protected voidfinal voidrun()This method attempts to create / bind anEntityManagerprior to execution.
-
Field Details
-
DEFAULT_ENTITY_MANAGER_NAME
- See Also:
-
-
Constructor Details
-
EntityManagerAwareRunnable
public EntityManagerAwareRunnable()Constructs an abstractRunnableimplementation that can be run in another thread. Guarantees that anEntityManageris bound to the current thread. -
EntityManagerAwareRunnable
Constructs an abstractRunnableimplementation that can be run in another thread. Guarantees that anEntityManageris bound to the current thread.If a
Semaphoreis 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 anEntityManagerprior to execution. This will also create and bind aBroadleafRequestContext, which should not already be bound to the background thread. If the run method is executed in a foreground thread, any existingBroadleafRequestContextbound to the thread will be returned to its previous state. -
executeInternal
Executes within the context of a thread-boundEntityManager. Helps avoidLazyInitializationExceptions and exceptions indicating that the Hibernate Session is closed.- Throws:
Exception
-
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
-