将连接对象返回到HikariCP池的最佳方法 [英] Best approach for returning connection objects to HikariCP pool

查看:71
本文介绍了将连接对象返回到HikariCP池的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HikariCP连接池。我能够使其正常工作并获得可以使用的连接。我不确定将连接返回到池的最佳方法是什么。

I am trying to use HikariCP connection pool. I was able to get it to work and get a connection that I could use. I am not sure what is the best approach for returning the connection to the pool.

我有以下问题:


  • 完成后应该关闭连接,还是依靠 idleTimeout
    maxLifetime 设置,还是还有另一个电话我可以使用
    来避免从池中窃取连接吗?

  • 如果我关闭连接(而不是返回池),
    不会是否会创建其他连接对象
    以满足连接池大小的要求?

  • Should I close the connection when I am done, rely on idleTimeout and maxLifetime settings or is there another call that I can use so as not to hog the connections from the pool?
  • If I close the connections (instead of returning to the pool), would that not result in additional connection objects being created to meet the requirements of the connection pool size?

寻找有用的建议。

推荐答案

与大多数连接池一样,当您询问时,Hikari不会为您提供实际的JDBC连接一。相反,它的作用是为您提供一个实现 Connection 接口的代理。对于Hikari,它是一个 ConnectionProxy 对象。

As with most connection pools, Hikari doesn't give you an actual JDBC Connection when you ask for one. What it does instead is give you a proxy that implements the Connection interface. In the case of Hikari - it's a ConnectionProxy object.

此代理有几个目的,主要目的是-使打开/关闭连接和语句的控制权远离您,进入连接池。 这是自动发生的,您应该照常使用连接。这包括使用后将其关闭。

This proxy serves a few purposes, the main of which is - take the control of opening/closing connections and statements away from you and into the connection pool. This happens automagically and you should be using your connections as usual. This includes closing them after use.

如果您查看 Hikari的源代码,尤其是在 ConnectionProxy 类中,您会看到 close()方法与标准之一。该代码显示为:

If you look at the source code for Hikari, at the ConnectionProxy class in particular, you will see that the close() method is very different from the standard one. The code reads as:


将连接标记为已关闭,进行清理,重置基础连接状态和参数。

Mark the connection as closed, do cleanup, reset underlying connection state and params.

因此,只需调用 close()即可清理并返回到池的连接。

Hence, simply calling close() will just clean and return the connection to the pool.

这篇关于将连接对象返回到HikariCP池的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆