首页 > 解决方案 > 将 Hibernate Search 与 Spring 应用程序一起使用时连接已关闭

问题描述

由于我在我的应用程序上添加了 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

我试过:设置一些光的属性,如:

 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有关,但老实说,我没有任何进一步的想法。

你们知道如何解决这个问题,甚至如何在短时间内重现这个错误吗?

谢谢。

标签: javahibernatetomcatconnection-poolingconnection-close

解决方案


帮助我们解决相同问题的步骤:

  • 升级你的 SpringBoot 版本。
  • 升级您的数据库连接器库版本。
  • 删除其他数据库连接池库,通常应该使用 SpringBoot 中的默认 Hikari。

推荐阅读