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

发布于 2024-08-24 01:48:20 字数 380 浏览 4 评论 0原文

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

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

When using a PreparedStatement in JDBC, should I close the PreparedStatement first or the Connection first? I just saw a code sample in which the Connection is closed first, but it seems to me more logical to close the PreparedStatement first.

Is there a standard, accepted way to do this? Does it matter? Does closing the Connection also cause the PreparedStatement to be closed, since the PreparedStatement is directly related to the Connection object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪都笑了 2024-08-31 01:48:20

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

  1. 结果集、
  2. 语句和
  3. 连接

(并沿途检查空值!)

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

如果您使用 Spring JdbcTemplate (或类似的)那么它将为您处理这个问题。或者,您可以使用 Apache Commons DbUtilsDbUtils.close() 或 < a href="http://commons.apache.org/dbutils/apidocs/org/apache/commons/dbutils/DbUtils.html#closeQuietly(java.sql.Connection,%20java.sql.Statement,%20java.sql. ResultSet)" rel="noreferrer">DbUtils.closeQuietly()

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

  1. the result set
  2. the statement
  3. the connection

(and check for nulls along the way!)

i.e. close in reverse order 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 DbUtils and DbUtils.close() or DbUtils.closeQuietly().

趴在窗边数星星i 2024-08-31 01:48:20

应完成以下过程(按顺序)

  • ResultSet
  • PreparedStatement
  • Connection

另外,建议在 finally close 中关闭所有 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());
        }
}

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

The following procedures should be done (in order)

  • The ResultSet
  • The PreparedStatement
  • The Connection.

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

//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 first if the connection is not autocommited. Many people fail to check it and realise that the transaction hasn't been committed to DB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文