搜索

首页  >  问答  >  正文

java - JPA连接数据库,一段时间没有数据操作会关闭连接,再次访问就会抛出异常,如何解决?

使用JPA规范的DataNucleus框架实现DAO层,如果一直保持连接,两天之后,会抛出异常,下面是配置:

        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/book_store?characterEncoding=utf-8"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="root"/>
        <property name="datanucleus.connectionPoolingType" value="tomcat"/>
        <property name="datanucleus.schema.autoCreateAll" value="true"/>
        <property name="datanucleus.connectionPool.maxPoolSize" value="5"/>
        <property name="datanucleus.connectionPool.maxIdle" value="10"/>
        <property name="datanucleus.connectionPool.leakThreshold" value="1"/>
        <property name="datanucleus.connectionPool.maxLifetime" value="240"/>
        <property name="datanucleus.schema.autoCreateAll" value="true"/>
        

这是报错信息:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.persistence.PersistenceException: No operations allowed after connection closed.
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:980)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
阿神阿神2777 天前723

全部回复(1)我来回复

  • PHPz

    PHPz2017-04-18 10:55:15

    你是不是关了 entityManager ?

     entitymanager.close();
    

    确保类似这种的放在后面

    回复
    0
  • 取消回复