我刚刚开始使用BoneCP并从作者站点中提取示例JDBC代码 .
我有一个名为getConnection()的函数返回一个连接,这里是一个片段:
// setup the connection pool
BoneCPConfig config = new BoneCPConfig();
// Config goes here.
connectionPool = new BoneCP(config); // setup the connection pool
return connectionPool.getConnection(); // fetch a connection
现在,我的问题:1)当我完成使用从上面的函数返回的连接时,我是否调用connection.close(),以便它返回到池中,或者它是否完全关闭连接?如何返回池连接?
2)如何在应用程序退出时清理池?我结束时会调用connectionPool.shutdown()吗?而且,我读到某个地方我需要单独关闭所有池化连接?这是真的?
谢谢 .
2 回答
从池中获取的Connection对象是一个包装类 . 它甚至可以在 Exception 中正确维护底层连接 .
即使在连接相关的异常中,例如 TERMINATE_ALL_CONNECTIONS ,BoneCP池也会正确关闭所有底层连接 .
总之,BoneCP池使缓存透明 . 客户方只需遵循立场流程,
请求连接( take the connection from pool ,池将决定是否重新使用/创建一个)
请求PreparedStatement / CallableStatement( reuse the object from pool if it is enabled )
执行语句
关闭声明,( release the statement object to the pool if it is enabled )
关闭连接,( release the connection object to the pool )
应用程序停止时,关闭池以释放所有缓存的连接 .
1. 始终调用
connection.close()
以返回与池的连接(它赢得了't be physically closed) when you' .2. 当您完成游泳池并且没有计划再次重新连接时,请致电
connectionPool.shutDown()
.