Java 7 Automatic Resource Management JDBC (try-with-resources statement)

六月ゝ 毕业季﹏ 提交于 2019-11-26 12:07:01

问题


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)

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();
}

With Java 7 you can go for:

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

   ...
   con.commit();
}

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.

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


回答1:


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
   }
}

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:

  • 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

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.




回答2:


IMO, declaring Connection and PreparedStatement outside try-catch is the best way available in this case.




回答3:


If you want to use pooled connection in transaction, you should use it in this way:

try (Connection conn = source.getConnection()) {
        conn.setAutoCommit(false);
        SQLException savedException = null;
        try {
            // Do things with connection in transaction here...
            conn.commit();
        } catch (SQLException ex) {
            savedException = ex;
            conn.rollback();
        } finally {
            conn.setAutoCommit(true);
            if(savedException != null) {
                throw savedException;
            }
        }
    } catch (SQLException ex1) {
        throw new DataManagerException(ex1);
    }

This sample code handles setting autocommit values.



来源:https://stackoverflow.com/questions/9260159/java-7-automatic-resource-management-jdbc-try-with-resources-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!