UF.SQLOBJ

当尝试使用已被释放的资源时,就会报告 UF(使用已释放)问题。UF.SQLOBJ 警告表明尝试使用已关闭的 JDBC 对象(比如语句、预定义语句或结果集)。

示例 1

复制
    public List<String> order() {
         final List<String> strings = new ArrayList<String>();
         populate(strings, 1, 3, 5, 7, 9);
         return strings;
     }
 
     public void populate(List<String> data, int... keys) {
         try {
             try {
                 ps = conn.prepareStatement("SELECT * FROM Table where key=?");
                 for (int key : keys) {
                     populate(data, key);
                 }
             } finally {
                 ps.close();
             }
         } catch (SQLException e) {
             // do nothing
         }
     }
 
     public void populate(List<String> data, int key) {
         try {
             ps.setInt(1, key);
             final ResultSet rs = ps.executeQuery();
             try {
                 while (rs.next()) {
                     String s = rs.getString(1);
                     data.add(s);
                 }
             } finally {
                 rs.close();
             }
         } catch (SQLException e) {
             try {
                 ps.close();
             } catch (SQLException e1) {
                 // do nothing
             }
         }
     }

针对第 39 行的代码段报告 UF.SQLOBJ:循环的每次迭代都会在第 39 行调用方法 populate(List<String> data, int... keys)。在引发 SQLException 的情况下(请参阅第 63 行),该方法会关闭预定义语句 ps。这可能导致在周期的下一次迭代中尝试使用已关闭的准备语句。