且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

经过长时间的I/O操作并发请求后,HikariPool连接不可用

更新时间:2022-01-30 17:33:09

您告诉过您:

一些高并发错误

您会得到:

java.sql.SQLTransientConnectionException:HikariPool-1-连接为不可用,请求在30005毫秒后超时.

java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30005ms.

这意味着数据库连接池(Hikari)未能在30秒内提供连接.
您将池的最大连接数配置为2:

That means that the DB connection pool (Hikari) didn't manage to provide a connection in 30 secs max.
You configured the max number of connection of the pool to 2 :

  maximumPoolSize: 2

而且,如果查询的处理时间很长并且应用程序具有很高的并发性,那么该参数看起来确实被低估了.
将其增加到更大的数字,例如10或更多,然后观察行为.您也可以使用connectionTimeout参数,但是如果客户端请求等待连接,结果将减慢您的应用程序的运行速度.

It is few and moreover, if your queries are quite long processing and that your application has high concurrency, that parameter looks really under-estimate.
Increase it to a higher number such as 10 or more and watch the behavior. You can also play with the connectionTimeout parameter but as a result, it will slow down your app if client requests wait for connection.

Hikari池大小文档可以为您提供一些有关调整配置的方式.

The Hikari pool size documentation could give you some information about the way to tweak the configuration.

作为替代方案,您可以重新考虑您的数据源.例如,定义两个数据源:一个用于长连接(用于长查询),另一个用于排序连接(用于短查询),并根据情况使用更合适的数据源.

As alternative, you could rethink your datasource. For example define two datasources : one for long connections (for long queries) and another one for sort connections (for short queries) and use the datasources that fits better according to the case.

作为旁注,Spring Boot提供了一个专注于数据源连接的执行器: DataSourceHealthIndicator ,用于检查是否可以获取与DataSource的连接.
结合使用监视系统工具(Prometheus或任何其他工具),这些工具可能有助于诊断此类问题.

As a side note Spring Boot provides an actuator focused on datasource connections : DataSourceHealthIndicator that checks that a connection to DataSource can be obtained.
Joined to a monitoring system tool (Prometheus or any other), these may be helpful to diagnostic that kind of issues.