【问题标题】:How to reconnect database if the connection closed in spring jpa?如果在spring jpa中连接关闭,如何重新连接数据库?
【发布时间】:2016-12-05 02:26:21
【问题描述】:

我在我的 web 应用程序中使用 spring-boot、spring-jpa、mysql。当我的应用程序运行几个小时时,我总是遇到以下异常:

2016-07-30 21:27:12.434 ERROR 13553 --- [http-nio-8090-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper   : No operations allowed after connection closed.
2016-07-30 21:27:12.434  WARN 13553 --- [http-nio-8090-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08003
2016-07-30 21:27:12.434 ERROR 13553 --- [http-nio-8090-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper   : No operations allowed after connection closed.
2016-07-30 21:27:12.438 ERROR 13553 --- [http-nio-8090-exec-8] [.[.[.[.c.c.Go2NurseJerseyConfiguration] : Servlet.service() for servlet [com.cooltoo.config.Go2NurseJerseyConfiguration] in context with path [] threw exception [org.springframework.dao.DataAccessResourceFailureException: could not prepare statement; nested exception is org.hibernate.exception.JDBCConnectionException: could not prepare statement] with root cause

java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
        at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3119) ~[mysql-connector-java-5.1.25.jar!/:na]
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3570) ~[mysql-connector-java-5.1.25.jar!/:na]
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3559) ~[mysql-connector-java-5.1.25.jar!/:na]
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4110) ~[mysql-connector-java-5.1.25.jar!/:na]
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570) ~[mysql-connector-java-5.1.25.jar!/:na]
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731) ~[mysql-connector-java-5.1.25.jar!/:na]
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815) ~[mysql-connector-java-5.1.25.jar!/:na]
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) ~[mysql-connector-java-5.1.25.jar!/:na]
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322) ~[mysql-connector-java-5.1.25.jar!/:na]
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82) ~[hibernate-core-4.3.11.Final.jar!/:4.3.11.Final]

我检查了数据库运行良好。发生这种情况时,我必须重新启动我的 spring-boot 应用程序。如何检查问题所在?为什么数据库连接被关闭了?如果发生这种情况,是否可以重新连接数据库? 下面是我的application.properties:

spring.datasource.url=jdbc:mysql://192.168.99.100:3306/test?characterEncoding=utf8
spring.datasource.username=admin
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=150

【问题讨论】:

    标签: java mysql spring spring-boot spring-data-jpa


    【解决方案1】:

    这似乎是 MySQL 的常见错误。

    1) 将其添加到您的 application.properties 中,看看效果如何:

    spring.datasource.testOnBorrow=true
    spring.datasource.validationQuery=SELECT 1
    

    testOnBorrowspring doc 和这个other stackoverflow question 中有详细说明。但是,我无法在 Spring 的文档中找到关于 validationQuery 的参考,但它似乎可以解决问题。

    2) 或者,您可以按照此处的建议使用 testWhileIdle http://christoph-burmeister.eu/?p=2849

    他建议将此添加到您的 application.properties:

    spring.datasource.testWhileIdle = true
    spring.datasource.validationQuery = SELECT 1
    

    在另一个 stackoverflow 问题中也提到了这个解决方案,它只是不被接受的答案,但似乎是某些人的解决方案。

    3) 在这种情况下,他们还添加了 timeBetweenEvictionRunsMillis

    spring.datasource.testWhileIdle = true
    spring.datasource.validationQuery = SELECT 1
    spring.datasource.timeBetweenEvictionRunsMillis = 3600000
    

    编辑:另一个 stackoverflow question 涵盖了这个(有一个非常完整的答案)

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      相关资源
      最近更新 更多