Docker MySQL在超时后丢弃表:连接关闭后不允许进行任何操作



一个非常奇怪的情况。我使用的是Spring Boot和Spring Data JPA以及在docker容器中运行的MySQL。启动应用程序后,一切正常(启动时使用spring.jpa.hibernate.ddl-auto=create-drop初始化DB(。

如果我让应用程序运行,大约10分钟后,当运行另一个请求时,我会返回table doesn't exist。检查数据库,我可以看到所有的表都不见了(架构仍然存在(!

日志显示错误前的警告:

2020-12-20 16:15:41.151  WARN 11018 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : myDS - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@67dd33b2 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-12-20 16:15:41.153  WARN 11018 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : myDS - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@3817c06d (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-12-20 16:15:41.155  WARN 11018 --- [nio-8080-exec-4] com.zaxxer.hikari.pool.PoolBase          : myDS - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@536cd1b2 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.

然后:

2020-12-20 16:15:41.161  WARN 11018 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1146, SQLState: 42S02
2020-12-20 16:15:41.161 ERROR 11018 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper   : Table 'tasker.account' doesn't exist
2020-12-20 16:15:41.173  INFO 11018 --- [nio-8080-exec-4] o.h.e.internal.DefaultLoadEventListener  : HHH000327: Error performing load command

我还在application.properties:中添加了此设置

spring.datasource.hikari.max-lifetime=600000以匹配mysql的设置。

Docker以启动

docker run --name=mysql1 --restart on-failure -d mysql/mysql-server:8.0

注意:mysql的本地实例(本机,不在docker中(永远不会发生这种情况

如有任何帮助,我们将不胜感激。

create-drop将在关闭SessionFactory时删除架构,现在让我们来处理SessionFactory关闭的部分,它可能与网络连接超时有关,导致SessionFactory被springboot自动关闭。

你可以在你的application.properties中添加一个心跳来防止这种行为的发生,无论如何,这个部分已经解决了我的生产环境中的很多问题,所以添加这个位是非常有用的:

#HeartBeat the database so that connections don't die otherwise the connections die silently
#and when a query commes along the JPA will throw an error and keep throwing errors and a restart of the process
#is inevitable
spring.datasource.testWhileIdle=true
spring.datasource.test-on-borrow=true
spring.datasource.validationQuery=SELECT 1

我将在这里放一个链接到一个答案,该答案处理检查连接和池健康连接到Db在>4<24在春季启动jpa休眠

另一件事是,由于这只是一个开发环境,您可以将create-drop更改为create,它将springboot配置为在启动springboot应用程序时删除现有的架构并创建一个新的架构。这样,当连接丢失时,架构永远不会被破坏。

看看这个答案:

spring.jpa.hibernate.ddl-auto属性在spring中究竟是如何工作的?

当你使用create-drop时,你可能对他的答案的以下部分感兴趣:

通常在测试用例场景中,您可能会使用create-drop,因此在创建模式时,测试用例添加了一些模拟数据运行测试,然后在测试用例清理过程中对象被丢弃,留下一个空数据库。

问题似乎出现在这个设置中:

spring.datasource.hikari.max-lifetime

在application.properties中添加此设置似乎解决了问题:spring.datasource.hikari.max-lifetime=100000而与mySQL值完全匹配的spring.datasource.hikari.max-lifetime=600000不起作用,即导致表被删除。

最新更新