我刚刚开始使用 BoneCP,这是我第一次使用连接池。我对我应该如何使用它感到有些困惑。目前我将 BoneCP 对象保存为静态变量,因此我可以在不同的连接之间使用它。
当我完成连接后,我用connection.close()
.
我应该这样做,还是不应该关闭它以使其能够被池重用?
这是我当前获取连接的实现:
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);
}
这看起来是正确的还是我误解了我应该如何使用 BoneCP?