DBCP 连接池 connection.close() return 是否连接到池

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

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

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

    }

我正在检查 BasicDataSource 的源代码,我找到了这个包装器 class 用于连接。

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 方法,该方法将关闭集合而不是 returning 到池的连接。这是 DBCP 的已知问题还是我错误地阅读了源代码请告诉我。

谢谢

你最终会调用 PoolableConnection.close(),returns 到池中。