Java Connection.close 是否回滚?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/218350/
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-10-29 11:28:23  来源:igfitidea点击:

Does Java Connection.close rollback?

javatransactionsdatabase-connection

提问by Antonio

Does Java Connection.close rollback into a finally block?.

Java Connection.close 是否回滚到 finally 块中?。

I know .Net SqlConnection.close does it.

我知道 .Net SqlConnection.close 做到了。

With this I could make try/finally blocks without catch...

有了这个,我可以在没有捕获的情况下进行 try/finally 块...

Example:

例子:

try {
    conn.setAutoCommit(false);
    ResultSet rs = executeQuery(conn, ...);
    ....
    executeNonQuery(conn, ...);
    ....

    conn.commit();
} finally {
   conn.close();
}

回答by Joel

According to the javadoc, you should try to either commit or roll back before calling the close method. The results otherwise are implementation-defined.

根据javadoc,您应该在调用 close 方法之前尝试提交或回滚。否则结果是实现定义的。

回答by Paul Tomblin

In any database system I've worked with, there is no harm in doing a rollback right after the commit, so if you commit in the try block, and rollback in the finally, things get committed, whereas if an exception or early return causes the commit to be missed, the rollback will rollback the transaction. So the safe thing to do is

在我使用过的任何数据库系统中,在提交后立即回滚没有坏处,所以如果你在 try 块中提交,并在 finally 中回滚,事情就会被提交,而如果异常或提前返回导致要错过的提交,回滚将回滚事务。所以安全的做法是

try {
    conn.setAutoCommit(false);
    ResultSet rs = executeQuery(conn, ...);
    ....
    executeNonQuery(conn, ...);
    ....

    conn.commit();
} finally {
   conn.rollback();
   conn.close();
}

回答by u7867

Oracle's JDBC driver commits on close() by default.You should not rely on this behaviour if you intend to write multi-platform JDBC code.

默认情况下,Oracle 的 JDBC 驱动程序在 close() 上提交。如果您打算编写多平台 JDBC 代码,则不应依赖此行为。

回答by lbergnehr

The behavior is completely different between different databases. Examples:

不同数据库之间的行为完全不同。例子:

Oracle

甲骨文

The transaction is committed when closing the connection with an open transaction (as @Mr. Shiny and New 安宇 stated.

在关闭与打开的事务的连接时提交事务(如@Shiny 先生和新安宇所说。

SQL Server

数据库服务器

Calling the close method in the middle of a transaction causes the transaction to be rolled back.

在事务中间调用 close 方法会导致事务回滚。

close Method (SQLServerConnection)

关闭方法(SQLServerConnection)

回答by Adnan Memon

For MySQL JDBC, the implementation rolls back the connection if closed without a call to commit or rollback methods.

对于 MySQL JDBC,如果在没有调用 commit 或 rollback 方法的情况下关闭连接,则实现会回滚连接。

回答by Mean

It is useless to rollback in finally block. After you commit, and commit is successful, why to roll back? So if i were you, i would rollback in catch block.

在 finally 块中回滚是没有用的。你commit后,commit成功,为什么要回滚?所以如果我是你,我会在 catch 块中回滚。