Transaction timeout override not cleared at end of request on thread used to run a servlet in JBoss EAP
Environment
- Red Hat JBoss Enterprise Application Platform EAP 7
Issue
- After a transaction timeout override is set (e.g. using Content from jakarta.ee is not included.UserTransaction.setTransactionTimeout(int)) during execution of a servlet, the timeout override remains bound on the thread.
- Subsequent servlets run on the same thread will unexpectedly encounter the non-default transaction timeout override.
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
Tags
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.