休眠 java.lang.IllegalStateException: 事务已经激活

Hibernate java.lang.IllegalStateException: Transaction already active

我正在使用 Java EE + Hibernate 5,最近我发现我的 Tomcat 9 日志中有一些错误消息说:

java.lang.IllegalStateException: Transaction already active

问题涉及的方法有:

// 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
    }
}

编辑。这是完整的堆栈跟踪(一些消息是西班牙语):

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)

我查看了堆栈跟踪,嗯...它说事务已经处于活动状态并且您正在尝试打开一个新事务。

这个已经激活的交易在哪里打开?

  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)

它必须在此处的某处打开(下面的所有内容都是系统 类 并且在您的控制之外)显式(通过方法调用)或隐式(@Transactional 注释或类似的)

您可以找到这段代码,它会在您调用方法之前打开事务并将其删除。或者您可以更改您的代码,以便它可以通过执行 .

之类的操作来加入现有交易