BoneCP correct usage

jim picture jim · Feb 17, 2012 · Viewed 8.4k times · Source

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.

Answer

wwadge picture wwadge · Feb 17, 2012

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.