hikari池连接不可用错误

ac1kyiln  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(697)

我的服务器上有一个spring启动项目。我有时会遇到“hikari池连接不可用”错误。我想不出原因。我看起来有很多这样的问题。一些人建议关闭一些@交易。但是,在我的应用程序中,我没有使用任何@transactional注解。以下是我的hikari池配置:


# hikari settings

spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=600000
spring.datasource.hikari.maxLifetime=1800000
spring.datasource.hikari.maximumPoolSize=10

# hikari log

logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
logging.level.com.zaxxer.hikari=TRACE

当我检查catalina.out日志文件时,我注意到活动连接数有时会增加。例如,昨天没有活动的连接。所有连接都处于空闲状态。现在池中有2个活动连接。我得到这个错误是因为连接池被所有活动的连接填充了吗?
顺便说一下,我使用的是mysql数据库。我应该设置maxlifetime比mysql wait\u timeout少一秒或两秒吗?我问这个问题是因为https://github.com/brettwooldridge/hikaricp,maxlifetime应该比任何数据库或基础结构强加的连接时间限制短几秒。你认为maxlifetime和等待超时有关吗?
以下是错误跟踪:

Caused by: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
        at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:48)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
        at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:109)
        at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:136)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:50)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:149)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:176)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:151)
        at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2082)
        at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2012)
        at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1990)
        at org.hibernate.loader.Loader.doQuery(Loader.java:949)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:351)
        at org.hibernate.loader.Loader.doList(Loader.java:2787)
        at org.hibernate.loader.Loader.doList(Loader.java:2770)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2604)
        at org.hibernate.loader.Loader.list(Loader.java:2599)
        at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338)
        at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2254)
        at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1069)
        at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:170)
        at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1506)
        ... 116 more
Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30001ms.
        at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:676)
        at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:190)
        at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:155)
        at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128)
qf9go6mv

qf9go6mv1#

根据您的描述,一个可能的原因是您的数据连接超过了最大池大小(设置为10)。造成这种情况的根本原因可能是代码中的连接泄漏。你把接头关好了吗?或者将其显式关闭为:

statement.close();
connection.close();

try (Connection connection =***;
PreparedStatement statement =***)
catch (SQLException ex) {***}

如果是这样,您可以启用hikari日志来检查。logback.xml文件

<logger name="com.zaxxer.hikari" level="debug" additivity="false">
    <appender-ref ref="STDOUT"/>
</logger>

相关问题