我注意到,即使数据库关闭,池中实际上没有可用的连接,Hikari CP 仍然会等待连接超时到期,然后再向客户端发送异常。
我同意当数据库可用时这是可取的,但就我而言,我希望池在没有可用连接时发送异常之前不要等待。
原因是数据库本身的响应时间不到 2 毫秒,因此我每秒可以处理数千个事务,但当没有可用连接时,池将等待更长的时间(建议的最小可接受超时为 250 毫秒),因此我无法再处理吞吐量。另一方面,我的逻辑可以在没有数据库的情况下工作一段时间。
我应该如何处理这个问题?
编辑:
This link几乎是我想要实现的目标,减去我希望 HikariCP 自动执行此操作的事实,我不应该激活挂起状态。
最佳答案
也许您应该在应用程序代码中的某个位置引入一个计数器,如果并发请求数超过该值,则不要使用数据库。如果不知道你正在处理什么,就很难判断,例如读与写。
根据 brettwooldridge comment regarding connectionTimeout
property由于线程调度,较低的超时是不可靠的,即使有可用的连接:
We can certainly consider a lower floor, but 125ms would be the absolute minimum.
Both Windows and Linux have a default scheduler quantum of 20ms. If 16 threads are running on a 4-core CPU, a single thread may have to wait up to 80ms just to be allowed to run. If the pool has a vacancy due to, for example, the retirement of a connection at maxLifetime, this leaves precious little time to establish a connection to fill the slot without returning a spurious failure to the client.
If careful consideration is not taken to ensure the CPU and scheduler are not oversaturated, running at a 125ms timeout, puts your application tier at risk of spurious failures even if the pool has available connections. For example, running 32 threads on a 4-core CPU can lead to thread starvations under load as long as 120ms -- very close to the edge.
关于java - 无可用连接时的 HikariCP 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56289567/