BoneCP: getConnection 方法是线程安全的还是我们必须处理它?
在使用 BoneCP 连接池时,我遇到了以下困惑,希望听到一些建议:
getConnection
BoneCP 线程安全的方法?当有许多线程请求并行连接时,使用它的最佳方法是什么?- 每次使用连接后都需要调用
connection.close()
吗? - 如果需要调用
connection.close()
,它是真的断开与数据库的连接还是只是将连接发送到池中?
预先感谢您的支持。
While using the BoneCP connection pooling, I came across the following confusions and would like to hear some advise on this:
- Is the
getConnection
method of the BoneCP thread-safe? What is the best way to use this when there are many threads asking for a connection in parallel? - Is it required to call
connection.close()
every time after using the connection? - if it is required to call
connection.close()
, will it really disconnect the connection with the DB or just sends the connection to the pool?
Thanks in advance for the support.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BoneCP的
getConnection()
是线程安全的;所以你不需要自己做任何事情。是的,如果连接完成,您需要调用
connection.close()
(这不是特定于 BoneCP,而是适用于任何 JDBC 连接)。与所有连接池一样,调用
connection.close()
会将连接返回到连接池,或者在某些情况下还会关闭物理连接(但实际上:这是一个您不应该关心的实现细节关于)。通常,连接池维护与数据库的物理连接池。当您调用
getConnection()
时,连接池会查找可用的物理连接并将其包装在逻辑连接中。返回此逻辑连接。当您close()
逻辑连接时,连接池知道物理连接可以再次重用。The
getConnection()
of BoneCP is thread-safe; so you don't have to do anything yourself.And yes, you need to call
connection.close()
if you are done with the connection (this is not specific to BoneCP, but applies to any JDBC connection).As with all connection pools, calling
connection.close()
will return the connection to the connection pool or in some cases also close the physical connection (but really: this is an implementation detail you should not be concerned about).In general connection pools maintain a pool of physical connections to a database. When you call
getConnection()
, the connection pool looks for an available physical connection and wrap it in a logical connection. This logical connection is returned. When youclose()
the logical connection, the connection pool knows the physical connection is available again for reuse.