Created
September 4, 2014 15:21
-
-
Save iliocatallo/e3369ef7bc9446919f37 to your computer and use it in GitHub Desktop.
EntityManagerInterceptor
This file contains hidden or 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
| import java.io.IOException; | |
| import javax.persistence.EntityTransaction; | |
| import javax.servlet.Filter; | |
| import javax.servlet.FilterChain; | |
| import javax.servlet.FilterConfig; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.ServletRequest; | |
| import javax.servlet.ServletResponse; | |
| public class EntityManagerInterceptor implements Filter { | |
| @Override | |
| public void destroy() {} | |
| @Override | |
| public void init(FilterConfig fc) throws ServletException {} | |
| @Override | |
| public void doFilter(ServletRequest req, ServletResponse res, | |
| FilterChain chain) throws IOException, ServletException { | |
| try { | |
| EntityManagerHelper.beginTransaction(); | |
| chain.doFilter(req, res); | |
| EntityManagerHelper.commit(); | |
| } catch (RuntimeException e) { | |
| EntityTransaction tx = EntityManagerHelper.getTransaction(); | |
| if (tx != null && tx.isActive()) | |
| EntityManagerHelper.rollback(); | |
| throw e; | |
| } finally { | |
| EntityManagerHelper.closeEntityManager(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment