Using BoneCP: Handling Pool Connections

I just started using BoneCP, and this is my first time using the connection pool. I am a bit confused about how I should use this. I am currently saving a BoneCP object as a static variable, and therefore I can use it between different connections.

When I finished the connection, I will close it connection.close() .
Should I do this, or should I not close it so that it can be reused by the pool?

This is my current implementation to get the connection:

 private static BoneCP connectionPool; public Connection getConnection() throws SQLException { if (connectionPool == null) { initPool(); } return connectionPool.getConnection(); } private void initPool() throws SQLException { BoneCPConfig config = new BoneCPConfig(); config.setJdbcUrl(DB_URL); config.setUsername(DB_USERNAME); config.setPassword(DB_PASSWORD); config.setMinConnectionsPerPartition(5); config.setMaxConnectionsPerPartition(10); config.setPartitionCount(1); connectionPool = new BoneCP(config); } 

Does this seem correct or did I misunderstand how should I use BoneCP?

+7
source share
1 answer

In addition to making your private static final and changing init to a static block (or, alternatively, forcing getConnection synchronization), you're fine.

You are right, you MUST make connection.close () to return to the pool. When your application shuts down, disconnect the connection pool

+9
source

All Articles