2016-07-30 20 views
3

I feder Boot verwende, feder JPA, MySQL in meiner Web-application.When meine Bewerbung für ein paar Stunden läuft, habe ich immer unter Ausnahmen bekam:Wie man Datenbank wiederverbindet, wenn die Verbindung im Frühjahr jpa geschlossen wurde?

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] 

ich, dass die Datenbank auch überprüft haben, läuft . Ich muss meine Spring-Boot-Anwendung neu starten, wenn das passiert ist. Wie kann ich überprüfen, was das Problem ist? Warum wurde die Datenbankverbindung geschlossen? Wenn das passiert ist, ob ich die Datenbank wieder verbinden kann? Unten ist mein 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 

Antwort

5

Dieses wie ein häufiger Fehler bei MySQL scheint.

1) Fügen Sie diese auf Ihrem application.properties und sehen, wie es geht:

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

testOnBorrow im spring doc und dieser other stackoverflow question detailliert. Ich bin jedoch nicht in der Lage, einen Verweis auf ValidationQuery in Spring Doc zu finden, aber es scheint den Trick zu tun.

2) Oder Sie testWhileIdle hier vorgeschlagene Verwendung als http://christoph-burmeister.eu/?p=2849

Er schlägt vor, diese zu Ihrem application.properties:

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

Diese Lösung auch in anderen Stackoverflow erwähnt wird Frage, es war einfach nicht die akzeptierte Antwort, aber scheint die Lösung für einige zu sein.

3) In diesem Fall sie fügte auch timeBetweenEvictionRunsMillis:

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

EDIT: Eine andere stackoverflow question, die diese (mit einer sehr vollständige Antwort deckt)