我正在使用 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.
那么,我做错了什么。?还是对配置有误解?
光初始化日志:
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
找到了这个日志。在这个线程中研究 了 HikariCP 连接中的 Reset (autoCommit) 。尝试auto commit
在两侧(休眠和 Hikari)设置相同(true)并在两侧尝试使用 false。仍然没有运气。启用
leakDetectionThreshold
,得到泄漏检测异常。所以试图了解休眠/弹簧事务管理器是否释放连接。从下面的日志来看,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
形式Hikari观点。因此,当数据库有 5 个空闲连接时,toatal = 5, active=4, idle = ,waiting=0
Hikari 日志中有。
笔记:
可能是我遇到了这个确切的问题https://github.com/brettwooldridge/HikariCP/issues/109在我的情况下,活动连接随着每笔交易而增加。
HikariCP - 连接不可用这也是同样的问题。但没有人为此提供明确的解决方案。顺便说一句,我
@Transactional
按照接受的答案的建议从乞讨中使用。