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 void
Executes within the context of a thread-boundEntityManager
.protected org.springframework.context.ApplicationContext
protected final jakarta.persistence.EntityManager
protected jakarta.persistence.EntityManagerFactory
The name of the persistence unit.protected void
final void
run()
This method attempts to create / bind anEntityManager
prior to execution.
-
Field Details
-
DEFAULT_ENTITY_MANAGER_NAME
- See Also:
-
-
Constructor Details
-
EntityManagerAwareRunnable
public EntityManagerAwareRunnable()Constructs an abstractRunnable
implementation that can be run in another thread. Guarantees that anEntityManager
is bound to the current thread. -
EntityManagerAwareRunnable
Constructs an abstractRunnable
implementation that can be run in another thread. Guarantees that anEntityManager
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 anEntityManager
prior 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 existingBroadleafRequestContext
bound to the thread will be returned to its previous state. -
executeInternal
Executes within the context of a thread-boundEntityManager
. Helps avoidLazyInitializationException
s 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
-