I'm using Java EE Hibernate 5 and recently I can see I've got some error messages in my Tomcat 9 logs saying:
java.lang.IllegalStateException: Transaction already active
The methods involved in the issue are:
// The caller method
Pic p = picDAO.getById(1000);
// The Hibernate method
@Override
public Pic getById(Integer id) throws BDException {
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.getTransaction();
try {
tx.begin();
Pic p = (Pic) session.get(myClass, id);
tx.commit();
return p;
}
catch(........) {
// Exceptions here
}
}
Edit. Here is the full stack trace (some messages are in spanish):
12-Nov-2021 09:30:43.875 GRAVE [http-nio-8080-exec-10] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() para servlet [ViewPic] lanzó excepción
java.lang.IllegalStateException: Transaction already active
at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:74)
at project.dao.DAOGeneric.getById(DAOGeneric.java:268)
at project.controller.forwards.ViewPic.processRequest(ViewPic.java:86)
at project.controller.MainServlet.doGet(MainServlet.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at project.controller.filters.TileConfigFilter.doFilter(TileConfigFilter.java:58)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:769)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1722)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:829)
CodePudding user response:
I've taken a look at the stack trace and well ... it says that a transaction is already active and you are trying to open a new one.
Where is this transaction, which is already active opened then ?
at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:74)
at project.dao.DAOGeneric.getById(DAOGeneric.java:268)
at project.controller.forwards.ViewPic.processRequest(ViewPic.java:86)
at project.controller.MainServlet.doGet(MainServlet.java:38)
It must be opened somewhere here ( everything below is system classes and outside your control ) either explicitly (by method calls) or implicitly (@Transactional annotation or similar)
You can either find this peace of code, which opens the transaction before you method call and remove it. Or you can change your code so that it can join an existing transaction by doing something like this.