I was hoping jboss would start the transaction for me, when my http request 
happened, and then close it when it was done.  But no go.  So I wrote the 
servlet filter below, and it all works.  I just hoped the container would take 
care of it for me.  Maybe in the future, when we have more time, I will try out 
Seam.

  | public class TransactionFilter implements Filter {
  |    FilterConfig config = null;
  |    
  |     public void init(FilterConfig arg0) throws ServletException {
  |            config=arg0;
  |     }
  | 
  |     public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain) throws IOException, ServletException {
  |       
  |       InitialContext ctx;
  |       UserTransaction ut = null;
  |       try {
  |          ctx = new InitialContext();
  |          ut = (UserTransaction) ctx.lookup("UserTransaction");
  |          if(Status.STATUS_NO_TRANSACTION == ut.getStatus()) {
  |             ut.begin();
  |          } else {
  |             ut = null;
  |          }
  |       } catch (Exception e) {
  |          config.getServletContext().log("Error getting context and 
beginning transaction", e);
  |       }
  |       
  | 
  |       try {
  |          chain.doFilter(request, response);
  |          if((ut != null) && (Status.STATUS_ACTIVE == ut.getStatus())) {
  |             ut.commit();
  |          }
  |       } catch (Exception e) {
  |          config.getServletContext().log("Error in filter or commit", e);
  |          try {
  |             if((ut != null  && (Status.STATUS_ACTIVE == ut.getStatus()))) {
  |                ut.rollback();
  |             }
  |          } catch (Exception e1) {
  |             config.getServletContext().log("Error rolling back exception", 
e1);
  |          }
  |       } finally {
  |                
  |       }
  |    }
  | 
  |     public void destroy() {
  |     }
  | }

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3949670#3949670

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3949670


_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to