据说在使用完JDBC资源后关闭所有资源是一个好习惯。但是如果我有下面的代码,是否有必要关闭Resultset和Statement?

Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
    conn = // Retrieve connection
    stmt = conn.prepareStatement(// Some SQL);
    rs = stmt.executeQuery();
} catch(Exception e) {
    // Error Handling
} finally {
    try { if (rs != null) rs.close(); } catch (Exception e) {};
    try { if (stmt != null) stmt.close(); } catch (Exception e) {};
    try { if (conn != null) conn.close(); } catch (Exception e) {};
}

问题是关闭连接是否有效,或者是否会留下一些可用的资源。


当前回答

一些方便功能:

public static void silentCloseResultSets(Statement st) {
    try {
        while (!(!st.getMoreResults() && (st.getUpdateCount() == -1))) {}
    } catch (SQLException ignore) {}
}
public static void silentCloseResultSets(Statement ...statements) {
    for (Statement st: statements) silentCloseResultSets(st);
}

其他回答

我现在使用Oracle和Java。以下是我的观点:

您应该显式地关闭ResultSet和Statement,因为Oracle以前在关闭连接后仍然保持游标打开的问题。如果您不关闭ResultSet(游标),它将抛出一个错误,如超过最大打开游标。

我认为您在使用其他数据库时可能会遇到同样的问题。

完成后关闭ResultSet:

完成时关闭ResultSet 完成后立即关闭ResultSet对象 使用ResultSet对象甚至 对象关闭 ResultSet对象 close,显式关闭ResultSet 给垃圾收集器机会 尽早回忆 因为ResultSet对象可能占用 大量内存取决于查询。 ResultSet.close ();

如果你想要更紧凑的代码,我建议使用Apache Commons DbUtils。在这种情况下:

Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
    conn = // Retrieve connection
    stmt = conn.prepareStatement(// Some SQL);
    rs = stmt.executeQuery();
} catch(Exception e) {
    // Error Handling
} finally {
    DbUtils.closeQuietly(rs);
    DbUtils.closeQuietly(stmt);
    DbUtils.closeQuietly(conn);
}

我创建了以下方法来创建可重用的One Liner:

public void oneMethodToCloseThemAll(ResultSet resultSet, Statement statement, Connection connection) {
    if (resultSet != null) {
        try {
            if (!resultSet.isClosed()) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (statement != null) {
        try {
            if (!statement.isClosed()) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    if (connection != null) {
        try {
            if (!connection.isClosed()) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

我在父类中使用此代码,它继承给所有发送DB查询的类。我可以在所有查询上使用联机程序,即使我没有resultSet。方法负责按照正确的顺序关闭ResultSet、Statement、Connection。这是我的最终块的样子。

finally {
    oneMethodToCloseThemAll(resultSet, preStatement, sqlConnection);
}

连接是否可池并不重要。即使是可入池的连接也必须在返回池之前进行清洁。

“干净”通常意味着关闭结果集和回滚任何挂起的事务,但不关闭连接。否则,池化就失去了意义。

据我所知,在当前的JDBC中,resultset和语句实现了AutoCloseable接口。这意味着它们会在被摧毁或超出范围时自动关闭。