简体   繁体   English

在同一请求期间使用 HikariCP 获取后续连接似乎很慢

[英]Getting subsequent connections using HikariCP during same request seems slow

I have a Spring boot app that use HikariCP for Postgres connection pooling.我有一个 Spring 启动应用程序,它使用 HikariCP 进行 Postgres 连接池。

Recently I've set up tracing to collect some data how time is spent when handling a request to a specific endpoint.最近,我设置了跟踪以收集一些数据,了解在处理对特定端点的请求时所花费的时间。

My assumptions are that when using HikariCP:我的假设是,在使用 HikariCP 时:

  1. The first connection to the database while handling the request might be a bit slower处理请求时第一次连接到数据库可能会慢一点
  2. Subsequent connections to the database should be fast (< 10 ms)与数据库的后续连接应该很快(< 10 毫秒)

However, as the trace shows, the first connection is fast (< 10 ms).但是,如跟踪所示,第一个连接速度很快(< 10 毫秒)。 And while some subsequent connections during the same request handling are also fast (< 10 ms), I frequently see some subsequent connections taking 50-100ms, which seems quite slow to me, although I'm not sure if this is to be expected or not.虽然同一请求处理过程中的一些后续连接也很快(< 10 毫秒),但我经常看到一些后续连接需要 50-100 毫秒,这对我来说似乎很慢,尽管我不确定这是否是预期的或不是。

Is there anything I can configure to improve this behavior?我可以配置什么来改善这种行为吗?

Maybe good to know:也许很高兴知道:

  • The backend in question doesn't really see any other traffic right now, so it's only handling traffic when I manually send requests to it有问题的后端现在并没有真正看到任何其他流量,所以它只在我手动向它发送请求时处理流量
  • I've changed maximumPoolSize to 1 to rule out that the issue is that it uses different connections in the context of 1 request and that's what causes the issue.我已将 maximumPoolSize 更改为 1 以排除问题是它在 1 个请求的上下文中使用不同的连接,这就是导致问题的原因。 The same behavior is still seen.仍然可以看到相同的行为。

I use the default Hikari settings, I don't change them.我使用默认的 Hikari 设置,我没有更改它们。

轨迹截图

I do think something is wrong with your pool configuration or your usage of the pool if it takes roughly 10 ms to get an already initialized connection from your pool.如果从池中获取已初始化的连接大约需要 10 毫秒,我确实认为您的池配置或池使用有问题。 I would expect it to be sub-millisecond... Are you sure you are using the pool correctly?我希望它是亚毫秒......你确定你正在正确使用池吗?

Make sure you are using as new versions of pool and driver as possible, and make sure that connectionTestQuery is not set, as that would execute a query every time the connection is obtained from the pool.确保尽可能使用新版本的池和驱动程序,并确保未设置connectionTestQuery ,因为每次从池中获取连接时都会执行查询。 The defaults should be good enough for the rest of the settings.对于设置的 rest,默认值应该足够好。

Debug logs could be one thing help figure out what is happening, metrics on the pool another.调试日志可能是一回事,有助于弄清楚发生了什么,池中的指标是另一回事。 Have a look at Spring Boot Actuator, it will help you with that...看看 Spring Boot Actuator,它会帮助您...

To answer your actual question on how you can improve the situation given it actually takes roughly 10 ms to obtain a connection: Do not obtain and return the connection to the pool for every query... If you do not want to pass the connection around in your code, and if it suits your use case, you can make this happen easily by making sure your whole request is wrapped in a transaction.要回答关于如何改善这种情况的实际问题,因为它实际上需要大约 10 毫秒才能获得连接:不要为每个查询获取连接并将连接返回到池中......如果你不想传递连接在您的代码中,如果它适合您的用例,您可以通过确保将整个请求包装在事务中来轻松实现这一点。 See the Spring guide on managing transactions .请参阅有关管理事务的 Spring 指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM