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)
问题解答
回答1:你是不是關了 entityManager ?
entitymanager.close();
確保類似這種的放在後面