Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BoneCP correct usage

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.

like image 262
jim Avatar asked Feb 17 '12 11:02

jim


2 Answers

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.

like image 86
wwadge Avatar answered Nov 04 '22 09:11

wwadge


 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,

  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)
  3. execute the statements
  4. close statement, (release the statement object to the pool if it is enabled)
  5. close connection, (release the connection object to the pool)

When application stop, shutdown the pool to release all the cached connections.

boneCP.shutdown()
like image 28
Shen liang Avatar answered Nov 04 '22 10:11

Shen liang