public abstract class EntityManagerAwareRunnable extends Object implements Runnable
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).Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ENTITY_MANAGER_NAME |
Constructor and Description |
---|
EntityManagerAwareRunnable()
Constructs an abstract
Runnable implementation that can be run in another thread. |
EntityManagerAwareRunnable(Semaphore sem)
Constructs an abstract
Runnable implementation that can be run in another thread. |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
executeInternal()
Executes within the context of a thread-bound
EntityManager . |
protected org.springframework.context.ApplicationContext |
getApplicationContext() |
protected javax.persistence.EntityManager |
getEntityManager() |
protected javax.persistence.EntityManagerFactory |
getEntityManagerFactory() |
String |
getEntityManagerName()
The name of the persistence unit.
|
protected void |
registerError(Exception e) |
void |
run()
This method attempts to create / bind an
EntityManager prior to execution. |
public static final String DEFAULT_ENTITY_MANAGER_NAME
public EntityManagerAwareRunnable()
Runnable
implementation that can be run in another thread. Guarantees that an EntityManager
is bound
to the current thread.public EntityManagerAwareRunnable(Semaphore sem)
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.sem
- public final void run()
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.protected abstract void executeInternal() throws Exception
EntityManager
.
Helps avoid LazyInitializationException
s and exceptions indicating that the Hibernate Session is closed.Exception
public String getEntityManagerName()
protected javax.persistence.EntityManagerFactory getEntityManagerFactory()
protected org.springframework.context.ApplicationContext getApplicationContext()
protected final javax.persistence.EntityManager getEntityManager()
protected void registerError(Exception e)
Copyright © 2022. All rights reserved.