简体   繁体   English

BoneCP正确用法

[英]BoneCP correct usage

I've just started using BoneCP and pulled the sample JDBC code from the authors site. 我刚刚开始使用BoneCP并从作者站点中提取示例JDBC代码。

I have a function called getConnection() that returns a connection here is a snippet: 我有一个名为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

Now, my questions: 1) Do I call connection.close() when I am finished using the connection that is returned from above function so it is returned to the pool OR does this close the connection completely? 现在,我的问题:1)当我完成使用从上面的函数返回的连接时,我是否调用connection.close(),以便它返回到池中,或者它是否完全关闭连接? How do I return connection to pool? 如何返回池连接?

2) How to cleanup the pool on application quit? 2)如何在应用程序退出时清理池? Do I call connectionPool.shutdown() when i'm finishing up? 我结束时会调用connectionPool.shutdown()吗? And also, I read somewhere that I need to close all pooled connections individually? 而且,我读到某个地方我需要单独关闭所有池化连接? Is this true? 这是真的?

Thanks. 谢谢。

1. Always call connection.close() to return the connection to the pool (it won't be physically closed) when you're done with it. 1.完成后,请始终调用connection.close()以返回到池的连接(它不会物理关闭)。

2. Call connectionPool.shutDown() when you're completely done with the pool and not planning of reconnecting again. 2.当您完成池并且不再计划重新连接时,请调用connectionPool.shutDown()

 Connection connection = dbPool.getConnection();

The Connection object gotten from the pool, is a wrapper class. 从池中获取的Connection对象是一个包装类。 It will maintain the underlying connection properly even in the Exception . 它甚至可以在Exception中正确维护底层连接。

Even in the Connection related exceptions, for example, TERMINATE_ALL_CONNECTIONS , the BoneCP pool will properly close all the underlying connections. 即使在连接相关的异常(例如, TERMINATE_ALL_CONNECTIONS)中 ,BoneCP池也将正确关闭所有底层连接。

In summary, BoneCP pool make the cache transparent. 总之,BoneCP池使缓存透明。 Client side only need follow the stand flow, 客户方只需遵循立场流程,

  1. request the connection ( take the connection from pool , pool will decide whether to re-use/create one) 请求连接( 从池中获取连接 ,池将决定是否重新使用/创建一个)
  2. request the PreparedStatement/CallableStatement ( reuse the object from pool if it is enabled ) 请求PreparedStatement / CallableStatement( 如果启用了池中的对象,则重用该对象
  3. execute the statements 执行语句
  4. close statement, ( release the statement object to the pool if it is enabled ) close语句,( 如果启用了语句对象,则将其释放到池中
  5. close connection, ( release the connection object to the pool ) close connection,( 释放连接对象到池

When application stop, shutdown the pool to release all the cached connections. 应用程序停止时,关闭池以释放所有缓存的连接。

boneCP.shutdown()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM