Я часто получаю это исключение :
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not prepare statement
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1423)
at org.hibernate.query.Query.getResultList(Query.java:146)
at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:72)
at com.inmobi.programmatics.myservice.dao.impl.AbstractDAOImpl.getByIds(AbstractDAOImpl.java:117)
Caused by: org.hibernate.exception.GenericJDBCException: could not prepare statement
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1940)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1909)
Caused by: java.sql.SQLException: Connection is closed
at com.zaxxer.hikari.pool.ProxyConnection$ClosedConnection$1.invoke(ProxyConnection.java:469)
at com.sun.proxy.$Proxy66.prepareStatement(Unknown Source)
at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:310)
at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
Вот моя конфигурация для использования HikariCP с JPA Hibernate:
JpaPersistModule jpaPersistModule = new JpaPersistModule("persistenceConfig");
final Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.hikari.dataSource.url",
"jdbc:postgresql://" + configurationData.getDBHost() + ":" + configurationData.getDBPort()
+ "/" + configurationData.getDBName() + "?stringtype=unspecified");
properties.put("hibernate.hikari.dataSource.user", configurationData.getDBUser());
properties.put("hibernate.hikari.dataSource.password", configurationData.getDBPassword());
properties.put("hibernate.hikari.minimumIdle", "2");
properties.put("hibernate.hikari.maximumPoolSize", "8");
properties.put("hibernate.hikari.connectionTimeout", "30000");
properties.put("hibernate.hikari.idleTimeout", "60000");
properties.put("hibernate.hikari.poolName", "master_pool");
properties.put("hibernate.hikari.leakDetectionThreshold", "60000");
properties.put("hibernate.hikari.maxLifetime", "600000");
properties.put("hibernate.hikari.connectionTestQuery", "select 1");
properties.put("hibernate.hikari.connectionInitSql", "select 1");
Вот мое постоянство.xml для JPA:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="persistenceConfig" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL94Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.jdbc.time_zone" value="UTC"/>
<property name="hibernate.jdbc.batch_size" value="25"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider"/>
<property name="hibernate.hikari.dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource"/>
</properties>
</persistence-unit>
</persistence>
Я проверил, что idle_in_transaction_session_timeout в postgres db равен 1 часу.Это означает, что нет проблем со стороны БД.
Я попытался настроить конфигурацию hikaricp.Но это не помогает.
Сначала я подумал, что это может быть из-за утечки соединения, для которой я добавил leakDetectionThreshold к 60 с.
Затем я подумал, что это может быть из-заполучение неверного соединения из пула, для которого я установил connectionTestQuery .Но это также не помогает.
Я проверил, что в любое время активно максимум 1 или 2 соединения.Так что это не может быть проблема размера maxpool.
Я изучил этот stackoverflow .Это не имеет никакого смысла для моей постановки проблемы, так как я использую entitymanager с Guice Transactional. Поэтому мне не нужно заботиться о закрытии соединения, которое позаботится Guice Transactional.
Может ли кто-нибудь помочь с этим?