Guest User

Untitled

a guest
Nov 28th, 2011
55
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package fi.affecto.osti.fwkint.servlet;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.inject.Inject;
  6. import javax.servlet.Filter;
  7. import javax.servlet.FilterChain;
  8. import javax.servlet.FilterConfig;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletRequest;
  11. import javax.servlet.ServletResponse;
  12. import javax.servlet.annotation.WebFilter;
  13. import javax.transaction.Status;
  14. import javax.transaction.SystemException;
  15. import javax.transaction.UserTransaction;
  16.  
  17. @WebFilter(value = "/*")
  18. public class RollbackFilter implements Filter
  19. {
  20. @Inject
  21. private UserTransaction userTransaction;
  22.  
  23. @Override
  24. public void destroy()
  25. {
  26. }
  27.  
  28. @Override
  29. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
  30. {
  31. try
  32. {
  33. rollbackTransactionIfActive();
  34. chain.doFilter(request, response);
  35. }
  36. finally
  37. {
  38. rollbackTransactionIfActive();
  39. }
  40. }
  41.  
  42. private void rollbackTransactionIfActive()
  43. {
  44. try
  45. {
  46. if (userTransaction.getStatus() == Status.STATUS_ACTIVE)
  47. {
  48. userTransaction.rollback();
  49. }
  50. }
  51. catch (SystemException e)
  52. {
  53. // so not?
  54. }
  55. }
  56.  
  57. @Override
  58. public void init(FilterConfig config) throws ServletException
  59. {
  60. }
  61.  
  62. }
  63.  
  64.  
RAW Paste Data