本文介绍了使用BoneCP:处理池中的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用BoneCP,这是我第一次使用连接池。关于我应该如何使用它,我有点困惑。目前我将BoneCP对象保存为静态变量,因此我可以在不同的连接之间使用它。

I have just started using BoneCP and this is my first time using a connection pool. I'm somewhat confused as to how I am supposed to use it. Currently I am saving the BoneCP-object as a static variable, and thus I can use it between different connections.

当我完成连接后,我关闭它使用 connection.close()

我应该这样做,还是不应该关闭它以使它能够被池重用?

When I'm done with the connection, I close it with connection.close().
Should I do this, or should I not close it to enable it to be reused by the pool?

这是我目前获得连接的实现:

This is my current implementation to get a 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);
}

这看起来是正确的还是我误解了我应该如何使用BoneCP?

Does this seem correct or have I misunderstood how I am supposed to use BoneCP?

推荐答案

除了使你的私有静态最终并将init更改为静态块(或者使得你的getConnection同步),你没问题。

Other than making your private static final and changing the init to a static block (or alternaitvely making your getConnection synchronized), you are ok.

你说得对,你必须做connection.close()才能回到游泳池。当您的应用关闭时,请关闭连接池

You are correct you MUST do connection.close() to return to the pool. When your app shuts down, shut down the connection pool

这篇关于使用BoneCP:处理池中的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 01:13