DBCP连接池connection.close()是否将连接返回到池 [英] Does DBCP connection pool connection.close() return connection to pool

查看:380
本文介绍了DBCP连接池connection.close()是否将连接返回到池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果执行getConnection(),则使用DBCP的BasicDataSource,并在finally块中关闭连接,它将连接真正返回到池中还是关闭连接。我正在检查的代码段代码是这个

Using BasicDataSource from DBCP if we do a getConnection() and in the finally block we close the connection does it really return the connection to the pool or does it close the connection. The snippet code I am checking is this

try {
        Connection conn1 = getJdbcTemplate().getDataSource()
                .getConnection();
        //Some code to call stored proc 

    } catch (SQLException sqlEx) {
        throw sqlEx;
    } finally {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException ex1) {
            throw ex1;
        }

    }

我正在检查

private class PoolGuardConnectionWrapper extends DelegatingConnection {

    private Connection delegate;

    PoolGuardConnectionWrapper(Connection delegate) {
        super(delegate);
        this.delegate = delegate;
    }

    public void close() throws SQLException {
        if (delegate != null) {
            this.delegate.close();
            this.delegate = null;
            super.setDelegate(null);
        }
    }

委托对象(如果类型为java.sql.Connection) 。包装器代码调用委托的close方法,该方法将关闭集合,而不是将连接返回到池中。这是DBCP的已知问题,还是我错误地阅读了源代码,请让我知道。

The delegate object if of type java.sql.Connection. The wrapper code calls the close method of the delegate which will close the collection rather than returning the connection to pool. Is this a known issue with DBCP or am I reading the source code incorrectly please let me know.

谢谢

推荐答案

您最终将呼叫 PoolableConnection.close(),将其返回到池中。

You'll end up calling PoolableConnection.close(), which returns it to the pool.

这篇关于DBCP连接池connection.close()是否将连接返回到池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆