为什么不关闭CallableStatement?

时间:2019-10-02 17:01:19

标签: java jdbc

HibernateCallableStatement实现了AutoCloseable,因此在try / catch完成后应将其关闭。即使将CallableStatement传递给另一个类的资源块进行try,为什么还要保持连接打开?

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

0 个答案:

没有答案