I've just started using BoneCP and pulled the sample JDBC code from the authors site.
I have a function called getConnection() that returns a connection here is a snippet:
// 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? How do I return connection to pool?
2) How to cleanup the pool on application quit? Do I call connectionPool.shutdown() when i'm finishing up? 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.
2. Call connectionPool.shutDown()
when you're completely done with the pool and not planning of reconnecting again.
Connection connection = dbPool.getConnection();
The Connection object gotten from the pool, is a wrapper class. It will maintain the underlying connection properly even in the Exception.
Even in the Connection related exceptions, for example, TERMINATE_ALL_CONNECTIONS, the BoneCP pool will properly close all the underlying connections.
In summary, BoneCP pool make the cache transparent. Client side only need follow the stand flow,
When application stop, shutdown the pool to release all the cached connections.
boneCP.shutdown()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With