4

由于我在我的应用程序上添加了 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有关,但老实说,我没有任何进一步的想法。

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

谢谢。

4

2 回答 2

1

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

  • 升级你的 SpringBoot 版本。
  • 升级您的数据库连接器库版本。
  • 删除其他数据库连接池库,通常应该使用 SpringBoot 中的默认 Hikari。
于 2021-03-30T12:21:11.467 回答
0

通过重新创建 entityManager 对象,我解决了这个错误

this.entityManager = entityManagerFactory.createEntityManager();

所以您需要做的就是:捕获异常并重新创建 entityManager 对象,然后再次搜索

可选读取(手动索引推荐):当我通过 Jpa 将任何对象保存(创建/更新)到数据库时

repository.save(obj)

发现对象在休眠搜索(Lucien/Elastic Search/..)中没有索引,所以我必须在将它保存到数据库后手动索引对象(但我不必在删除时手动删除索引对象它来自数据库)。

repository.save(objFromDB);
manualIndexToHibernateSearch(objFromDB);
...
public void manualIndexToHibernateSearch(MyObject objFromDB) {
    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
    fullTextEntityManager.getTransaction().begin();
    //find object by id in hibernate-search 
    MyObject objFromHibernateSearch = fullTextEntityManager.find(MyObject.class, objFromDB.getId());
    //you have to replace 'text' field with your fields that have @Field annotation
    objFromHibernateSearch.setText(objFromDB.getText());
    //index
    fullTextEntityManager.index(objFromHibernateSearch);
    //commit
    fullTextEntityManager.getTransaction().commit();
}
于 2021-02-22T06:18:58.943 回答