首页 > 解决方案 > 为什么这不关闭 CallableStatement?

问题描述

HibernateCallableStatement 实现了 AutoCloseable 所以一旦 try/catch 完成就应该关闭它。为什么即使将 CallableStatement 传递给另一个类中的 try with resources 块,连接也会保持打开状态?

protected CallableStatement fetchCallableStatement() throws SQLException {
    HibernateCallableStatement hcs = new HibernateCallableStatement(this.getDBPackageStatement().getStatement());
    this.cs = hcs.getCallableStatement();
    for (Param param : getLoadedPackageStatement().getParams()) {
        if (param instanceof In) {
            this.cs.setObject(param.getPosition(), param.getValue());
        } else {
            this.cs.registerOutParameter(param.getPosition(), (int) param.getValue());
        }
    }
    this.cs.execute();
    return this.cs;
}    

try (CallableStatement cs = this.fetchCallableStatement()) {
        //CallableStatement Building...
    } catch (SQLException ex) {
        Logger.getLogger(CursorFetcher.class.getName()).log(Level.SEVERE, null, ex);
    }

标签: javajdbc

解决方案


推荐阅读