Skip to content

Instantly share code, notes, and snippets.

@dilnei
Last active August 29, 2015 14:04
Show Gist options
  • Save dilnei/3ce730775ad390ae2ee0 to your computer and use it in GitHub Desktop.
Save dilnei/3ce730775ad390ae2ee0 to your computer and use it in GitHub Desktop.
JPAFilter for web aplication (web-profile) action-based
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() {
}
}
@dilnei
Copy link
Author

dilnei commented Aug 3, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment