Java 我应该先关闭哪一个,PreparedStatement 还是 Connection?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2363785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:35:41  来源:igfitidea点击:

Which should I close first, the PreparedStatement or the Connection?

javajdbcconnectionprepared-statement

提问by froadie

When using a PreparedStatementin JDBC, should I close the PreparedStatementfirst or the Connectionfirst? I just saw a code sample in which the Connectionis closed first, but it seems to me more logical to close the PreparedStatementfirst.

PreparedStatement在 JDBC 中使用 a时,我应该关闭第PreparedStatement一个还是第Connection一个?我刚刚看到一个代码示例,其中Connection首先关闭了,但在我看来,关闭第一个更合乎逻辑PreparedStatement

Is there a standard, accepted way to do this? Does it matter? Does closing the Connectionalso cause the PreparedStatementto be closed, since the PreparedStatementis directly related to the Connectionobject?

有没有标准的、公认的方法来做到这一点?有关系吗?关闭 是否Connection也会导致PreparedStatement关闭,因为PreparedStatementConnection对象直接相关?

采纳答案by Brian Agnew

The statement. I would expect you to close (in order)

该声明。我希望你关闭(按顺序)

  1. the result set
  2. the statement
  3. the connection
  1. 结果集
  2. 该声明
  3. 连接

(and check for nulls along the way!)

(并在此过程中检查空值!)

i.e. close in reverseorder to the opening sequence.

即以与打开顺序相反的顺序关闭。

If you use Spring JdbcTemplate(or similar) then that will look after this for you. Alternatively you can use Apache Commons DbUtilsand DbUtils.close()or DbUtils.closeQuietly().

如果您使用 Spring JdbcTemplate(或类似的),那么它会为您处理这个问题。或者,您可以使用Apache Commons DbUtilsDbUtils.close()DbUtils.closeQuietly()

回答by Buhake Sindi

The following procedures should be done (in order)

应执行以下程序(按顺序)

  • The ResultSet
  • The PreparedStatement
  • The Connection.
  • ResultSet
  • PreparedStatement
  • Connection

Also, it's advisable to close all JDBC related objects in the finallyclose to guarantee closure.

此外,建议在finally关闭以保证关闭时关闭所有与 JDBC 相关的对象。

//Do the following when dealing with JDBC. This is how I've implemented my JDBC transactions through DAO....

Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;

try {
  conn = ....
  ps = conn.prepareStatement(...);

  //Populate PreparedStatement
  rs = ps.executeQuery();

} catch (/*All relevant exceptions such as SQLException*/Exception e) {
  logger.error("Damn, stupid exception: " , e);
} finally {
if (rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
                logger.error(e.getMessage(), e.fillInStackTrace());
            }
        }

        if (ps != null) {
            try {
                ps.close();
                ps = null;
            } catch (SQLException e) {
                logger.error(e.getMessage(), e.fillInStackTrace());
            }
        }

        try {
            if (conn!= null && !conn.isClosed()){
                if (!conn.getAutoCommit()) {
                    conn.commit();
                    conn.setAutoCommit(true);
                }
                conn.close();
                conn= null;
            }
        } catch (SQLException sqle) {
            logger.error(sqle.getMessage(), sqle.fillInStackTrace());
        }
}

You can see I've checked if my objects are null and for connection, check firstif the connection is not autocommited. Many people fail to check it and realise that the transaction hasn't been committed to DB.

你可以看到我已经检查过我的对象是否为空,对于连接,首先检查连接是否没有自动提交。许多人没有检查它并意识到事务尚未提交给数据库。