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

Sai*_*aif 10 spring hibernate spring-boot hikaricp connection-pool

我使用的是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
Run Code Online (Sandbox Code Playgroud)

我期望的是,空闲连接应在30000ms/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
Run Code Online (Sandbox Code Playgroud)

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

  1. SpringBootJPAHikariCP - Reset (autoCommit) on connection org.postgresql.jdbc.PgConnection@1344bbf1找到了这个日志.研究了 在HikariCP这个线程中连接时的重置(autoCommit).试图auto commit在两侧(hibernate和Hikari)设置相同(true)并且在两侧尝试使用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
    
    Run Code Online (Sandbox Code Playgroud)
  3. 所有空闲连接都是idle形式上postgres的观点并active形成Hikari的观点.所以当数据库有5个空闲连接时,toatal = 5, active=4, idle = ,waiting=0 Hikari日志中有.

注意:

  1. 可能是我有这个确切的问题https://github.com/brettwooldridge/HikariCP/issues/109在我的情况下活动连接增加每个事务.

  2. HikariCP - 连接不可用这也是一个相同的问题.但没人提供明确的解决方案.顺便说一下,我@Transactional从接受的答案所建议的乞讨中使用.

Sai*_*aif 6

这不是光的任何问题,我的最后一个错误。仍会发布这种情况的详细信息,以防万一。

我正在使用spring boot 1.5.6(这是我开始工作时的最新版本)。此版本包括在内spring-orm 4.3.1。这个版本的spring-orm包括支持三个版本的Hibernate, Hibernate5Hibernate4Hibernate3

因此,我为配备了波纹管配置的弹簧靴current_session_context_class

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
Run Code Online (Sandbox Code Playgroud)

一切正常,直到涉及到Hikari的连接管理为止。发生spring-boot-starter-jpa了什么事,包括了1.5.6 Hibernate5(我的意思是休眠核心)。

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

变更后

org.springframework.orm.hibernate4.SpringSessionContext
Run Code Online (Sandbox Code Playgroud)

org.springframework.orm.hibernate5.SpringSessionContext
Run Code Online (Sandbox Code Playgroud)

该问题立即得到解决。

我当前的配置是

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
Run Code Online (Sandbox Code Playgroud)

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