public abstract class JpaTransactionFilter extends Object implements javax.servlet.Filter
EntityManager and deal with the complete lifecycle of a JPA
entityManager while a request.
The injected JPA entityManager will be closed (if was really opened) at the end of
the request.
excludeMethods: This parameters configure a set of method names
which should never be called on the proxied entityManager.
When a such method is called on the entityManager then the filter will pass in
the hook onExcludeMethod(Object, Method, Object[]).
Default implementation of this hook is to throw an exception.
unusedMethods: This parameters configure a set of method names
which should be by-pass when the proxied entityManager was not still open
(via a EntityManager#.
When a such method is called on the entityManager then the filter will pass in
the hook onUnusedMethod(Object, Method, Object[]).
Default implementation of this hook is to not return null values.
requestAttributeName
(default value is JPA_TRANSACTION_REQUEST_ATTRIBUTE) and can be
changed.
A convience method is created here to obtain the entityManager getTransaction(ServletRequest) :
EntityManager em = JpaTransactionFilter.getTransaction(ServletRequest);If you prefer to not use this nice method, you can also do this:
EntityManager em = (EntityManager) request.getAttribute(JpaTransactionFilter#JPA_TRANSACTION_REQUEST_ATTRIBUTE);Or
EntityManager em = (EntityManager) request.getAttribute(modifiedAttributeName);
| Modifier and Type | Class and Description |
|---|---|
class |
JpaTransactionFilter.JpaTransactionProxyInvocationHandler
Handler of a proxy on a
EntityManager. |
| Modifier and Type | Field and Description |
|---|---|
static String[] |
DEFAULT_EXCLUDE_METHODS |
static String[] |
DEFAULT_UNUSED_METHODS |
protected Set<String> |
excludeMethods
names of methods to forbid access while using proxy.
|
static String |
JPA_TRANSACTION_REQUEST_ATTRIBUTE |
protected String |
requestAttributeName
Name of the request attribute where to push the entityManager.
|
protected Set<String> |
unusedMethods
names of methods to by-pass if no entityManager opened on proxy.
|
| Constructor and Description |
|---|
JpaTransactionFilter() |
| Modifier and Type | Method and Description |
|---|---|
protected abstract javax.persistence.EntityManager |
createEntityManager(javax.servlet.ServletRequest request)
Method to open a new entityManager.
|
void |
destroy() |
void |
doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain) |
Set<String> |
getExcludeMethods() |
static javax.persistence.EntityManager |
getTransaction(javax.servlet.ServletRequest request) |
Set<String> |
getUnusedMethods() |
void |
init(javax.servlet.FilterConfig filterConfig) |
protected void |
onCloseTransaction(javax.persistence.EntityManager entityManager)
Hook method to close the topia entityManager of the request at the end of
the request when all filter has been consumed.
|
protected Object |
onExcludeMethod(Object proxy,
Method method,
Object[] args)
Hook method called when a method with his name in
excludeMethods was invoked on the proxied entityManager. |
protected Object |
onUnusedMethod(Object proxy,
Method method,
Object[] args)
Hook method called when a method with his name in
unusedMethods was invoked on the proxied entityManager
while the underlying entityManager is still not opened. |
void |
setRequestAttributeName(String requestAttributeName)
to change the
requestAttributeName. |
public static final String JPA_TRANSACTION_REQUEST_ATTRIBUTE
public static final String[] DEFAULT_EXCLUDE_METHODS
public static final String[] DEFAULT_UNUSED_METHODS
protected Set<String> excludeMethods
protected Set<String> unusedMethods
protected String requestAttributeName
JPA_TRANSACTION_REQUEST_ATTRIBUTE.public void setRequestAttributeName(String requestAttributeName)
requestAttributeName.requestAttributeName - new name of the request attribute
where to push the entityManager.public static javax.persistence.EntityManager getTransaction(javax.servlet.ServletRequest request)
protected abstract javax.persistence.EntityManager createEntityManager(javax.servlet.ServletRequest request)
request - incoming requestpublic void destroy()
destroy in interface javax.servlet.Filterpublic void init(javax.servlet.FilterConfig filterConfig)
throws javax.servlet.ServletException
init in interface javax.servlet.Filterjavax.servlet.ServletExceptionpublic void doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain)
throws IOException,
javax.servlet.ServletException
doFilter in interface javax.servlet.FilterIOExceptionjavax.servlet.ServletExceptionprotected Object onExcludeMethod(Object proxy, Method method, Object[] args) throws Throwable
excludeMethods was invoked on the proxied entityManager.proxy - proxied entityManagermethod - method invokedargs - arguments of the invoked methodThrowable - if any error to do.protected void onCloseTransaction(javax.persistence.EntityManager entityManager)
entityManager - the entityManager to close (can be null if entityManager
was not required while the current request)protected Object onUnusedMethod(Object proxy, Method method, Object[] args) throws Throwable
unusedMethods was invoked on the proxied entityManager
while the underlying entityManager is still not opened.proxy - the proxy itselfmethod - method invokedargs - arguments of the invoked methodThrowable - if any error to do.Copyright © 2011–2013 CodeLutin. All rights reserved.