Spring Boot Jooq 如何在同一个 gRPC 调用中使用相同的连接?

huangapple go评论69阅读模式
英文:

Spring boot jooq how to use same connection in a same grpc call?

问题

我们正在使用Hikari CP,我发现在同一个gRPC调用中多次重复调用getConnectionclose(它会将连接返回到连接池)。

如果连接有限,这会影响性能,getConnection将会变得非常慢。

这将导致某些调用的性能变得很慢,使得调用的P99性能变得糟糕。

我需要将调用标记为只读事务以优化重复的getConnection调用以进行查询吗?或者我可以使用某些gRPC服务器拦截器来执行此类操作吗?

谢谢。

英文:

We are using hikari cp, and I have found repeated call getConnection and close(which returns the connection to the connection pool) multiple times in same grpc call.

If the connection is limited and it will impact the performance the getConnection will be too slow.

It will make some calls performance too slow. And make the p99 of calls terrible.

Need I mark the call transactional with readonly to optimize the repeated getConnection call for the queries? Or maybe I can use some kind grpc server interceptors to do such trick?

Thanks.

答案1

得分: 2

jOOQ的 ConnectionProvider 是这样设计的:

包装JDBC DataSource的默认实现会有效地调用:

  • DataSource::getConnection
  • Connection::close

这是对于jOOQ查询而言最明智的默认设置,它对于你的应用程序、事务、生命周期等一无所知。

在Spring中,也在jOOQ中,一旦你启动了一个事务,Connection 就会与线程绑定,这意味着不是为每个查询获取新的连接,而是为每个查询提供相同的连接(这是你的代码必须具备的事务要求)。

因此,确实,使你的代码具备事务性将防止为每个jOOQ查询重新获取新的连接。无论你使用Spring的事务还是jOOQ的事务,它们都是这样工作的。作为第三种选择,你还可以手动管理JDBC Connection 的生命周期,并向jOOQ提供一个 Connection 而不是 DataSource

这也在这里有详细记录

英文:

jOOQ's ConnectionProvider is designed this way:

The default implementation that wraps a JDBC DataSource will effectively call:

  • DataSource::getConnection
  • Connection::close

This is the most sensible default for a jOOQ query, which knows nothing about your application, transactions, lifecycles, etc.

In Spring, but also in jOOQ, once you start a transaction, the Connection becomes thread-bound, meaning that instead of acquiring a new connection for every query, the same one is provided for each query (this being a prerequisite for your code being transactional).

So, indeed, making your code transactional will prevent re-acquiring new connections for every jOOQ query. It doesn't matter if you're using Spring's transactions, or jOOQ's. They both work this way. As a third option, you can also manually manage the JDBC Connection lifecycle and supply jOOQ with a Connection instead of a DataSource.

This is all documented here as well.

huangapple
  • 本文由 发表于 2023年6月8日 08:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定