英文:
Spring boot jooq how to use same connection in a same grpc call?
问题
我们正在使用Hikari CP,我发现在同一个gRPC调用中多次重复调用getConnection
和close
(它会将连接返回到连接池)。
如果连接有限,这会影响性能,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
是这样设计的:
- 在每次查询之前,会调用
ConnectionProvider.acquire():Connection
- 在每次查询之后,会调用
ConnectionProvider.release(Connection)
包装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:
- Before each query,
ConnectionProvider.acquire():Connection
is called - After each query,
ConnectionProvider.release(Connection)
is called
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论