org.springframework.orm.jpa.support
public class: OpenEntityManagerInViewFilter [javadoc |
source]
java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
All Implemented Interfaces:
Filter, DisposableBean, BeanNameAware, InitializingBean, ServletContextAware
Servlet 2.3 Filter that binds a JPA EntityManager to the thread for the
entire processing of the request. Intended for the "Open EntityManager in
View" pattern, i.e. to allow for lazy loading in web views despite the
original transactions already being completed.
This filter makes JPA EntityManagers available via the current thread,
which will be autodetected by transaction managers. It is suitable for service
layer transactions via org.springframework.orm.jpa.JpaTransactionManager
or org.springframework.transaction.jta.JtaTransactionManager as well
as for non-transactional read-only execution.
Looks up the EntityManagerFactory in Spring's root web application context.
Supports a "entityManagerFactoryBeanName" filter init-param in web.xml;
the default bean name is "entityManagerFactory". Looks up the EntityManagerFactory
on each request, to avoid initialization order issues (when using ContextLoaderServlet,
the root application context will get initialized after this filter).
| Field Summary |
|---|
| public static final String | DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME | |
| Methods from org.springframework.web.filter.GenericFilterBean: |
|---|
|
addRequiredProperty, afterPropertiesSet, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setServletContext |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter Detail: |
protected EntityManager createEntityManager(EntityManagerFactory emf) {
return emf.createEntityManager();
}
|
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
EntityManagerFactory emf = lookupEntityManagerFactory(request);
boolean participate = false;
if (TransactionSynchronizationManager.hasResource(emf)) {
// Do not modify the EntityManager: just set the participate flag.
participate = true;
}
else {
logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewFilter");
try {
EntityManager em = createEntityManager(emf);
TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em));
}
catch (PersistenceException ex) {
throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
}
}
try {
filterChain.doFilter(request, response);
}
finally {
if (!participate) {
EntityManagerHolder emHolder = (EntityManagerHolder)
TransactionSynchronizationManager.unbindResource(emf);
logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewFilter");
EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
}
}
}
|
protected String getEntityManagerFactoryBeanName() {
return this.entityManagerFactoryBeanName;
}
Return the bean name of the EntityManagerFactory to fetch from Spring's
root application context. |
protected EntityManagerFactory lookupEntityManagerFactory() {
if (logger.isDebugEnabled()) {
logger.debug("Using EntityManagerFactory '" + getEntityManagerFactoryBeanName() +
"' for OpenEntityManagerInViewFilter");
}
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
return wac.getBean(getEntityManagerFactoryBeanName(), EntityManagerFactory.class);
}
Look up the EntityManagerFactory that this filter should use.
The default implementation looks for a bean with the specified name
in Spring's root application context. |
protected EntityManagerFactory lookupEntityManagerFactory(HttpServletRequest request) {
return lookupEntityManagerFactory();
}
|
public void setEntityManagerFactoryBeanName(String entityManagerFactoryBeanName) {
this.entityManagerFactoryBeanName = entityManagerFactoryBeanName;
}
Set the bean name of the EntityManagerFactory to fetch from Spring's
root application context. Default is "entityManagerFactory". |