gpt4 book ai didi

java - setAutoCommit(true) 是否提交过去的执行?

转载 作者:行者123 更新时间:2023-11-30 11:09:51 26 4
gpt4 key购买 nike

public void batchUpdate() throws SQLException {

Statement stmt = null;
try {
this.connection.setAutoCommit(false);
stmt = this.connection.createStatement();


stmt.addBatch("INSERT INTO COFFEES VALUES('Amaretto_decaf', 49, 10.99, 0, 0)");

stmt.addBatch("INSERT INTO COFFEES " +"VALUES('Hazelnut_decaf', 49, 10.99, 0, 0)");

int [] updateCounts = stmt.executeBatch();
this.connection.commit();

} catch(BatchUpdateException b) {
JDBCTutorialUtilities.printBatchUpdateException(b);
} catch(SQLException ex) {
JDBCTutorialUtilities.printSQLException(ex);
} finally {
if (stmt != null) { stmt.close(); }
this.connection.setAutoCommit(true);
}
}

问题:

  1. 将调用 connection.setAutoCommit(true) 再次提交已经提交的语句(通过方法 connection.commit())还是仅恢复您不必调用的默认状态方法自己提交?

  2. 在不调用 connection.setAutoCommit(true) 的情况下关闭连接是否会自动调用该方法,或者下次它仍然是 connection.setAutoCommit(false)打开了吗?

最佳答案

  1. 已提交的语句不再提交。

  2. 这取决于您的连接源:连接池可能会在该状态下释放时为您提供 autoCommit=false 连接,新连接将以默认值打开。

    It is strongly recommended that an application explicitly commits or rolls back an active transaction prior to calling the close method. If the close method is called and there is an active transaction, the results are implementation-defined. http://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#close%28%29

关于java - setAutoCommit(true) 是否提交过去的执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27983748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com