UF.SQLCON

当尝试使用已被释放的资源时,就会报告 UF(使用已释放)问题。UF.SQLCON 警告表明尝试使用已关闭的 JDBC 连接。

示例 1

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

针对第 29 行的代码段报告 UF.SQLCON,因为在第 28 行调用的方法 populate 关闭了 JDBC 连接 conn,以防出现任何 SQLException。这意味着在第 29 行进行下一次 populate 调用时,将可能尝试使用已关闭的连接。