HikariCP空闲连接在连接池中保持活动状态

问题描述 投票:8回答:1

我使用的是Spring Boot(1.5.6),Hibernate,Postgres,Hikari(2.7.8)。我的配置是:

spring.datasource.hikari.minimumIdle=1
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=50000
spring.datasource.hikari.connectionTimeout=30000

我期望的是,空闲连接应在30000 ms / 30秒空闲后释放。 问题在于每个请求都建立了新的连接,保留所有空闲连接。所以经过一段时间我最终得到了20个空闲连接并且有了新的请求Hikari试图获得一个新的连接并获得SpringBootJPAHikariCP - Connection is not available, request timed out after 30001ms.

那么,我做错了什么。或者误解了配置?

Hikari初始化日志:

SpringBootJPAHikariCP - configuration:
 allowPoolSuspension.............false
 autoCommit......................true
 catalog.........................none
 connectionInitSql...............none
 connectionTestQuery.............none
 connectionTimeout...............30000
 dataSource......................none
 dataSourceClassName.............none
 dataSourceJNDI..................none
 dataSourceProperties............{password=<masked>}
 driverClassName................."org.postgresql.Driver"
 healthCheckProperties...........{}
 healthCheckRegistry.............none
 idleTimeout.....................30000
 initializationFailFast..........true
 initializationFailTimeout.......1
 isolateInternalQueries..........false
 jdbc4ConnectionTest.............false
 jdbcUrl.........................jdbc:postgresql://localhost:5432/dbname
 leakDetectionThreshold..........0
 maxLifetime.....................50000
 maximumPoolSize.................20
 metricRegistry..................none
 metricsTrackerFactory...........none
 minimumIdle.....................1
 password........................<masked>
 poolName........................"SpringBootJPAHikariCP"
 readOnly........................false
 registerMbeans..................false
 scheduledExecutor...............none
 scheduledExecutorService........internal
 schema..........................none
 threadFactory...................internal
 transactionIsolation............default
 username........................"postgres"
 validationTimeout...............5000

更新:在过去24小时内,我尝试了来自不同线程的多个解决方案,但没有一个解决了我的问题。所以这里的观察可能很重要。

  1. SpringBootJPAHikariCP - Reset (autoCommit) on connection org.postgresql.jdbc.PgConnection@1344bbf1找到了这个日志。研究了Reset (autoCommit) on connection in HikariCP这个帖子。试图在两侧设置auto commit相同(真实)(hibernate和Hikari)并且在两侧尝试使用false。仍然没有运气。
  2. 启用leakDetectionThreshold,获得泄漏检测异常。所以试图了解hibernate / spring事务管理器是否释放连接。从波纹管Logs看起来它看起来像hibernate正常工作。 28 22:19:35- DEBUG - o.s.orm.jpa.JpaTransactionManager-371 :: Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4212be39] for JPA transaction 28 22:19:35- DEBUG - o.h.e.t.internal.TransactionImpl-51 :: begin 28 22:19:35- DEBUG - o.s.orm.jpa.JpaTransactionManager-403 :: Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@243e942] 2com.someentity.MyEntity#ac918eed-345f-4a6c-8539-fe14e7fc41e2 28 22:19:35- DEBUG - o.h.r.j.i.LogicalConnectionManagedImpl-137 :: Initiating JDBC connection release from afterTransaction 28 22:19:35- DEBUG - c.zaxxer.hikari.pool.ProxyConnection-242 :: SpringBootJPAHikariCP - Executed rollback on connection org.postgresql.jdbc.PgConnection@1344bbf1 due to dirty commit state on close(). 28 22:19:35- DEBUG - o.h.e.i.AbstractFlushingEventListener-132 :: Processing flush-time cascades 28 22:19:35- DEBUG - o.h.e.i.AbstractFlushingEventListener-174 :: Dirty checking collections 28 22:19:35- DEBUG - org.hibernate.internal.SessionImpl-508 :: Disconnecting session 28 22:19:35- DEBUG - o.s.orm.jpa.JpaTransactionManager-759 :: Initiating transaction commit 28 22:19:35- DEBUG - o.s.orm.jpa.JpaTransactionManager-512 :: Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4212be39] 28 22:19:35- DEBUG - o.h.e.t.internal.TransactionImpl-62 :: committing 28 22:19:35- DEBUG - o.h.r.j.i.LogicalConnectionManagedImpl-137 :: Initiating JDBC connection release from afterTransaction 28 22:19:35- DEBUG - o.h.r.j.i.LogicalConnectionManagedImpl-137 :: Initiating JDBC connection release from afterTransaction 28 22:19:35- DEBUG - o.s.orm.jpa.JpaTransactionManager-600 :: Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4212be39] after transaction 28 22:19:35- DEBUG - o.s.o.jpa.EntityManagerFactoryUtils-435 :: Closing JPA EntityManager
  3. 所有空闲连接都是qazxsw poi形式qazxsw poi观点和idle形式Hikari观点。所以当数据库有5个空闲连接时,Hikari日志中有postgres

注意:

  1. 可能是我有这个确切的问题active在我的情况下活动连接增加每个交易。
  2. toatal = 5, active=4, idle = ,waiting=0这也是同一个问题。但没人提供明确的解决方案。顺便说一句,我接受了https://github.com/brettwooldridge/HikariCP/issues/109的乞讨,如接受的答案所示。
spring hibernate spring-boot hikaricp connection-pool
1个回答
6
投票

这不是任何Hikari问题,我最终有一个错误。仍然发布有关这种情况的详细信息,以防有人帮忙。

我正在使用HikariCP - connection is not available(这是我开始工作时的最新版本)。这个版本包括@Transactional。这个版本的spring boot 1.5.6包括对三种版本的hibernate,spring-orm 4.3.1spring-ormHibernate5的支持。

所以我为Hibernate4配置了波纹管配置的弹簧靴。

Hibernate3

一切都很好,直到Hikari的连接管理。发生了什么是current_session_context_class 1.5.6包括spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext (我的意思是hibernate核心)。

因此,执行任何DB操作后,弹簧将失去对该连接的控制(此版本不匹配的可能性最大)。因此问题。

改变之后

spring-boot-starter-jpa

Hibernate5

问题立即得到解决。

我目前的配置是

org.springframework.orm.hibernate4.SpringSessionContext

仅供参考,解决问题后切换到Spring Boot 2。

© www.soinside.com 2019 - 2024. All rights reserved.