1

假设我的代码大致类似于这样:(使用 oracle 10G jdbc)。在这种特定场景下会提交事务吗?

    public void someMethod(){
    try {
    OracleConnection connection = getConnectionFromPool();
    connection.setAutoCommit(false);

    // Do some transaction here - complete transaction, no errors occurred

    ...

    //Throw my own exception here
    throw new Exception("Custom Exception");


    } catch (Exception e}
    {
      ...
    }
    finally {
      connection.setAutoCommit(true);
    }
    }
4

2 回答 2

5

根据 JavaDocs,它应该提交:

注意:如果在事务期间调用此方法并且更改了自动提交模式,则提交事务。如果调用了 setAutoCommit 并且未更改自动提交模式,则该调用是无操作的。

但是:如果你依赖这个,这意味着你依赖驱动程序来遵守这个要求 - 这是我不会做的事情(我永远不会依赖隐含的事情)

如果您想确保您的事务已提交,请调用commit().

于 2013-03-15T08:15:45.557 回答
0

否。删除connection.setAutoCommit(false);

finally {
      connection.commit();
    }
于 2013-03-15T07:56:34.943 回答