【发布时间】:2020-04-10 23:20:13
【问题描述】:
自从我在我的应用程序中添加了 Hibernate Search 后,我似乎在一段时间后失去了与 Hikari 池的连接(似乎超过 8 小时)
我已经为这个错误苦苦挣扎了整整一周,但我真的不知道为什么会这样:
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not prepare statement
Caused by: org.hibernate.exception.GenericJDBCException: could not prepare statement
Caused by: java.sql.SQLException: Connection is closed
我试过了: 设置一些 hikari 的属性,例如:
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimumIdle=20
spring.datasource.hikari.maximumPoolSize=100
spring.datasource.hikari.idleTimeout=300000
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=200000
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.registerMbeans=true
将我的 bean 配置为使用如下所示的 testQuery:
public DataSource siteDataSource() {
HikariConfig config = new HikariConfig();
HikariDataSource dataSource;
config.setJdbcUrl(env.getProperty("spring.mysql.datasource.url"));
config.setUsername(env.getProperty("spring.mysql.datasource.username"));
config.setPassword(env.getProperty("spring.mysql.datasource.password"));
config.setDriverClassName(env.getProperty("spring.mysql.datasource.driver-class-name"));
config.setMaximumPoolSize(15);
config.setConnectionTestQuery("SELECT 1");
// performance senstive settings
config.setMinimumIdle(0);
config.setConnectionTimeout(30000);
config.setIdleTimeout(35000);
config.setMaxLifetime(45000);
dataSource = new HikariDataSource(config);
return dataSource;
}
(这个配置的东西也被删除了,错误还是一样)
并使用:
autoReconnect=true
我也收到了下面的错误,这似乎在当前的实现中消失了(但老实说,我不知道为什么 - 是的,我在这里很迷茫):
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: The last packet successfully received from the server was 50,090,585 milliseconds ago. The last packet sent successfully to the server was 50,090,585 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
我什至不知道如何重现此错误。我必须等待一整天才能为每个新测试再次测试它。
似乎是Tomcat + Hibernate + Hikari,但老实说,我没有任何进一步的想法。
你们知道如何解决这个问题,甚至如何在短时间内重现这个错误吗?
谢谢。
【问题讨论】:
-
我已经读过了。我已经尝试了他们建议的一切。我唯一没有尝试的事情(因为看起来非常糟糕)是让一个 cron 工作每小时发出一个虚假请求以保持连接活跃。
-
我找不到解决方案...
-
我假设您有一些代码不会将数据库连接返回到池。例如您在某处使用连接对象。 “从服务器成功接收到的最后一个数据包是 50,090,585 毫秒”可以与您锁定将心跳请求发送到数据库的线程相关联。我建议进行线程转储以检查线程状态。
-
我通过每分钟发出一个随机请求以保持连接处于活动状态来管理它。到目前为止,我没有发现其他更好的东西。
标签: java hibernate tomcat connection-pooling connection-close