Java 7自动资源管理JDBC(try-with-resources语句) [英] Java 7 Automatic Resource Management JDBC (try-with-resources statement)

查看:1756
本文介绍了Java 7自动资源管理JDBC(try-with-resources语句)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何集成创建/接收连接,查询数据库以及可能使用Java 7的自动资源管理(try-with-resources语句)处理结果的常用JDBC习惯用法? (教程

How to integrate the common JDBC idiom of creating/receiving a connection, querying the database and possibly processing the results with Java 7's automatic resource management, the try-with-resources statement? (Tutorial)

在Java 7之前,通常的模式是这样的:

Before Java 7, the usual pattern was something like this:

Connection con = null;
PreparedStatement prep = null;

try{
    con = getConnection();
    prep = prep.prepareStatement("Update ...");
    ...
    con.commit();
}
catch (SQLException e){
    con.rollback(); 
    throw e;
}
finally{
    if (prep != null)
        prep.close();
    if (con != null)
        con.close();
}

使用Java 7,您可以选择:

With Java 7 you can go for:

try(Connection con = getConnection(); PreparedStatement prep = con.prepareConnection("Update ..."){

   ...
   con.commit();
}

这将关闭连接 PreparedStatement ,但是回滚呢?我不能添加包含回滚的catch子句,因为连接是仅在try块中可用。

This will close the Connection and the PreparedStatement, but what about the rollback? I cannot add a catch clause containing the rollback, because the connection is only available within the try block.

您是否仍然在try块之外定义连接?这里的最佳做法是什么,特别是在使用连接池的情况下?

Do you still define the connection outside of the try block? What is the best practice here, especially if connection pooling is used?

推荐答案

try(Connection con = getConnection()) {
   try (PreparedStatement prep = con.prepareConnection("Update ...")) {
       //prep.doSomething();
       //...
       //etc
       con.commit();
   } catch (SQLException e) {
       //any other actions necessary on failure
       con.rollback();
       //consider a re-throw, throwing a wrapping exception, etc
   }
}

根据 oracle文档,您可以结合尝试-with-resources块与常规try块。 IMO,上面的示例捕获了正确的逻辑,即:

According to the oracle documentation, you can combine a try-with-resources block with a regular try block. IMO, the above example captures the correct logic, which is:


  • 如果没有出错,尝试关闭PreparedStatement

  • 如果内部区块出现问题,(无论是什么)回滚当前交易

  • 尝试关闭连接,无论什么

  • 如果关闭连接出现问题,则无法回滚事务(因为这是连接上的方法,现在处于不确定状态),所以不要尝试

  • Attempt to close the PreparedStatement if nothing goes wrong
  • If something goes wrong in the inner block, (no matter what is is) roll back the current transaction
  • Attempt to close the connection no matter what
  • If something goes wrong closing the connection, you can't rollback the transaction (as that's a method on the connection, which is now in indeterminate state), so don't try

在java 6及更早版本中,我会使用一组三层嵌套的try块(外部try-finally,中间try-catch,内部try-finally) 。 ARM语法确实做到了这一点。

In java 6 and earlier, I would do this with a triply nested set of try blocks (outer try-finally, middle try-catch, inner try-finally). ARM syntax does make this terser.

这篇关于Java 7自动资源管理JDBC(try-with-resources语句)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆