高并发情况下Hibernate添加操作时部分线程SessionException: Session is closed!

发布于 2022-09-05 01:01:14 字数 3774 浏览 13 评论 0

Servlet + hibernate在高并发情况下部分线程会出现org.hibernate.SessionException: Session is closed!,但是在单线程情况下不会出现。

org.hibernate.SessionException: Session is closed!
    at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72)
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1466)
    at sun.reflect.GeneratedMethodAccessor18.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
    at com.sun.proxy.$Proxy6.beginTransaction(Unknown Source)
    at cn.com.thit.pis.usermanager.dao.BaseDAO.queryByMainKey(BaseDAO.java:136)
    at cn.com.thit.pis.usermanager.service.PhoneService.Register(PhoneService.java:137)
    at cn.com.thit.pis.usermanager.servlet.Register.doGet(Register.java:52)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2430)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2419)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

BaseDAO.java

    public UserEntity queryByMainKey(long phone) {
        int retry = 0;
        entity = new UserEntity();
        session = factory.getCurrentSession();
        transaction = session.beginTransaction();
        while (retry++ < 3) {
            try {
                //if(!session.isOpen()) session.beginTransaction();
                Query q = session.createQuery("from UserEntity where phone=?");
                q.setLong(0, phone);

                if (q.list() == null || q.list().size() == 0) {
                    return null;
                }
                entity.Clone((UserEntity) q.list().get(0));

                return entity;
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
        return null;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬心 2022-09-12 01:01:15

连接池配少了吧

掩饰不了的爱 2022-09-12 01:01:15

解决了,将代码改成如下即可

public UserEntity queryByMainKey(long phone) {
        int retry = 0;
        entity = new UserEntity();
        Session session = factory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        while (retry++ < 3) {
            try {
                //if(!session.isOpen()) session.beginTransaction();
                Query q = session.createQuery("from UserEntity where phone=?");
                q.setLong(0, phone);

                if (q.list() == null || q.list().size() == 0) {
                    return null;
                }
                entity.Clone((UserEntity) q.list().get(0));

                return entity;
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
        return null;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文