Does DBCP connection pool connection.close() return connection to pool

老子叫甜甜 提交于 2019-12-06 02:33:01

问题


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;
        }

    }

I was checking the source code of BasicDataSource and I reached this wrapper class for the connection.

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);
        }
    }

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.

Thanks


回答1:


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



来源:https://stackoverflow.com/questions/28209955/does-dbcp-connection-pool-connection-close-return-connection-to-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!