Transaction timeout override not cleared at end of request on thread used to run a servlet in JBoss EAP

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform EAP 7

Issue

Resolution

This issue will be addressed in a future release1.

As a workaround, a custom WebFilter like the one illustrated below can be deployed to perform post-request cleanup of the thread-bound transaction override.

import java.io.IOException;

import javax.naming.InitialContext;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.transaction.TransactionManager;

@WebFilter("/*") // This may be refined for specific servlets where appropriate
public class TransactionInterceptor implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
                try {
                        txManager = (TransactionManager) new InitialContext().lookup("java:/TransactionManager");
                } catch (Throwable t) {
                        throw new ServletException("Failure initializing transaction manager reference", t);
                }
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
                try {
                        chain.doFilter(request, response);
                } catch (IOException|ServletException e) {
                        throw e;
                } catch (Throwable t) {
                        throw new ServletException("Failure running servlet", t);
                } finally {
                        try {
                                txManager.setTransactionTimeout(0);
                        } catch (Throwable resetFailure) {
                                // log the failure here
                        }
                }
        }

        @Override
        public void destroy() {
        }

        private TransactionManager txManager;
}

Root Cause

This is a known defect: This content is not included.JBEAP-22656 - There is no cleanup of thread bound transaction timeout override on threads used to run servlets

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.