Java connection.setAutoCommit = false 会发生什么

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

what happens on connection.setAutoCommit = false

javajdbcconnectionautocommit

提问by eatSleepCode

what happens if I do connection.setAutoCommit(false);does it creates a new transaction at database side?

如果我这样connection.setAutoCommit(false);做会在数据库端创建一个新事务会发生什么?

回答by Mick Mnemonic

According to the documentation, connection.setAutoCommit(false)will allow you to groupmultiple subsequent Statements under the same transaction. This transaction will be committed when connection.commit()is invoked, as opposed to after each execute()call on individual Statements (which happens if autocommit is enabled).

根据文档connection.setAutoCommit(false)将允许您在同一事务下对多个后续s进行分组Statement。该事务将在connection.commit()被调用时提交,而不是在每次execute()调用单个Statements 之后(如果启用自动提交就会发生)。

Changing the auto-commit mode through connection.setAutoCommit()will implicitly commit the active transaction and create a new one. From the Javadocs:

通过更改自动提交模式connection.setAutoCommit()将隐式提交活动事务并创建一个新事务。从Javadocs

NOTE: If this method is called during a transaction and the auto-commit mode is changed, the transaction is committed. If setAutoCommit is called and the auto-commit mode is not changed, the call is a no-op.

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

回答by drgPP

The JavaDocs provide a nice explanation of this use case in the Using Transactions Section

JavaDocs 在Using Transactions 部分对此用例提供了很好的解释

Disabling Auto-Commit Mode

When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. (To be more precise, the default is for a SQL statement to be committed when it is completed, not when it is executed. A statement is completed when all of its result sets and update counts have been retrieved. In almost all cases, however, a statement is completed, and therefore committed, right after it is executed.)

The way to allow two or more statements to be grouped into a transaction is to disable the auto-commit mode. This is demonstrated in the following code, where con is an active connection:

con.setAutoCommit(false);

禁用自动提交模式

创建连接时,它处于自动提交模式。这意味着每个单独的 SQL 语句都被视为一个事务,并在执行后立即自动提交。(更准确地说,默认情况下,SQL 语句在完成时提交,而不是在执行时提交。当检索到所有结果集和更新计数时,语句就完成了。然而,在几乎所有情况下, ,语句在执行后立即完成,因此被提交。)

允许两个或多个语句组合到一个事务中的方法是禁用自动提交模式。这在以下代码中进行了演示,其中 con 是活动连接:

con.setAutoCommit(false);

回答by jfcorugedo

The implementation of each method inside JDBC API depends on each driver. Oracle may do something very different than MySql does.

JDBC API 中每个方法的实现取决于每个驱动程序。Oracle 可能会做一些与 MySql 非常不同的事情。

However, only calling connection.setAutoCommit(false);doesn't creates a transaction. It only means that any statement created using this connection will be committed together when you call connection.commit();.

但是,仅调用connection.setAutoCommit(false);不会创建事务。这仅意味着使用此连接创建的任何语句将在您调用时一起提交connection.commit();

Take a look at thisOracle tutorial.

看看这个Oracle 教程。

回答by Nallamachu

Let me put it with simple explanation with code. When we have applied

让我用代码简单解释一下。当我们申请

Connection.setAutoCommit(false);

Connection.setAutoCommit(false);

in our source code, it will disable the autocommit option, which by default enable in database.

在我们的源代码中,它将禁用自动提交选项,默认情况下在数据库中启用。

So, you have to call

所以,你必须打电话

Connection.commit();

Connection.commit();

method explicitly to persist any changes to the database.

方法显式地保留对数据库的任何更改。

Class.forName(drivers);
Connection dbConnnection=DriverManager.getConnection(connectionURL,username,password);
dbConnection.setAutoCommit(false); //Disabling the Autocommit
Statement selectStatement = dbConnection.createStatement("Select Query");
ResultSet rs = selectStatement.execute();
while(rs.next()){
    Statement updateStatement = dbConnection.createStatement("update Query");
    //Apply some changes to update record
    statement.execute();
    dbConnection.commit();  //Mandatory to execute to persist changes into database
}