Last active
August 29, 2015 14:04
-
-
Save dilnei/3ce730775ad390ae2ee0 to your computer and use it in GitHub Desktop.
JPAFilter for web aplication (web-profile) action-based
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.app.util; | |
import java.io.IOException; | |
import javax.servlet.*; | |
/** | |
* JPA FIlter, Responsavel por filtrar as conexão. | |
* | |
* @author Dilnei Cunha. | |
*/ | |
public class JPAFilter implements Filter { | |
public JPAFilter() { | |
super(); | |
} | |
/** | |
* Se precisar de alguma coisa na inicialização devera ser feito aqui. | |
* | |
* @param filterConfig | |
* @throws ServletException | |
*/ | |
public void init(FilterConfig filterConfig) throws ServletException { | |
} | |
/** | |
* Aqui e aplicado o fitro para cada request e response. | |
* | |
* @param request | |
* @param response | |
* @param chain | |
* @throws IOException | |
* @throws ServletException | |
*/ | |
@Override | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | |
throws IOException, ServletException { | |
try { | |
JPAUtil.getEntityManager(); | |
chain.doFilter(request, response); | |
} finally { | |
JPAUtil.closeEntityManager(); | |
} | |
} | |
/* | |
* Não é necessario fazer nada. | |
*/ | |
public void destroy() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JPAFilter controla o ciclo de vida de uma EntityMamanger
Criei este filtro JPA para aplicações não CDI, a proposta deste JPAFilter é controlar a vida da EntityManager deixando-a em um escopo de request.