spring - HikariCP 空闲连接在连接池中保持事件状态

标签 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

我期望的是,空闲连接应该在 30000 之后释放。毫秒/30 秒的空闲时间。
问题在于每个请求都建立新连接,而所有空闲连接都保持原样。因此,经过一段时间后,我得到了 20 个空闲连接,并收到了一个新请求 Hikari 尝试获得一个新连接并获得 SpringBootJPAHikariCP - Connection is not available, request timed out after 30001ms.
那么,我做错了什么。?还是对配置有误解?

光初始化日志:
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 小时内,我尝试了来自不同线程的几种解决方案,但都没有解决我的问题。所以这里有一些可能很重要的观察结果。
  • SpringBootJPAHikariCP - Reset (autoCommit) on connection org.postgresql.jdbc.PgConnection@1344bbf1找到了这个日志。研究过
    Reset (autoCommit) on connection in HikariCP这个线程。试图设置auto commit两侧( hibernate 和光)相同(真),并在两侧也尝试使用假。仍然没有运气。
  • 启用 leakDetectionThreshold ,得到泄漏检测异常。所以试图了解 hibernate/ Spring 事务管理器是否释放连接。从下面的日志来看,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
    
  • 所有空闲连接a都是idle表格 postgres观点和 active 形成光的观点。所以当数据库有5个空闲连接时,有toatal = 5, active=4, idle = ,waiting=0在光日志中。

  • 注:
  • 可能是我遇到了这个确切的问题https://github.com/brettwooldridge/HikariCP/issues/109在我的情况下,每笔交易都会增加活跃连接。
  • HikariCP - connection is not available这也是同一个问题。但没有人为此提供明确的解决方案。顺便说一句,我正在使用 @Transactional从接受的答案所建议的乞讨中。
  • 最佳答案

    这不是任何光的问题,我的结果有一个错误。仍在发布有关如何发生的详细信息,以防它对某人有所帮助。

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

    所以我为 current_session_context_class 配置了带有波纹管配置的 Spring Boot 。 .

    spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
    

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

    因此,在执行任何数据库操作后, Spring 会失去对该连接的控制(此版本不匹配的可能性最大)。因此问题。

    改变后
    org.springframework.orm.hibernate4.SpringSessionContext
    


    org.springframework.orm.hibernate5.SpringSessionContext
    

    问题立即得到解决。

    我目前的配置是
    spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
    

    仅供引用,解决问题后切换到 Spring Boot 2。

    关于spring - HikariCP 空闲连接在连接池中保持事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50554636/

    相关文章:

    java - DB2 中的 JDBC 和 Hibernate 错误 : Connection authorization failure occurred | SQL Error: -99999, SQLState: 42505

    java - 为什么运行 mvn clean install 命令(在 Spring Boot 项目上)如果我注释掉这些方法来跳过测试,我也会遇到单元测试失败?

    java - Spring webflux 中的缓存

    java - 嵌入 Spring 的 ApacheDS

    java - HHH000513 : Unable to create the ReflectionOptimizer for Entity after upgrading to Hibernate 6

    java - 如何在 Spring 4.2 或更高版本中设置 Cache-control : private with applicationContext. xml

    java - 使用 hibernate 进行内连接

    java - "aggregate-oriented domain"意味着什么(谈到基于 Spring 的应用程序)?

    java - 以编程方式启用 Spring Security pre-post-annotations

    java - Spring 安全|授予匿名用户访问权限的问题