14.5.2.2 autocommit, Commit, and Rollback
作者:互联网
In InnoDB, all user activity occurs inside a transaction. If autocommit mode is enabled, each SQL statement forms a single transaction on its own. By default, MySQL starts the session for each new connection with autocommit enabled, so MySQL does a commit after each SQL statement if that statement did not return an error. If a statement returns an error, the commit or rollback behavior depends on the error. See Section 14.21.4, “InnoDB Error Handling”.
在innodb中,所有的用户行为都发生在事务中,如果开始了autocommit模式,每个SQL语句都自己形成一个事务。默认情况下,mysql在autocommit开启的情况下为每个新连接启动会话,因此,如果语句没有返回错误,会在每一个SQL语句后面做提交操作,如果语句返回了一个错误,那么进行提交还是回滚行为取决于返回的错误,具体信息请看Section 14.21.4, “InnoDB Error Handling”.
A session that has autocommit enabled can perform a multiple-statement transaction by starting it with an explicit START TRANSACTION or BEGIN statement and ending it with a COMMIT or ROLLBACK statement. See Section 13.3.1, “START TRANSACTION, COMMIT, and ROLLBACK Syntax”.
启用 autocommit 的会话可以通过以显式的START TRANSACTION或BEGIN语句启动并使用COMMIT或ROLLBACK语句结束它来执行多语句事务。
If autocommit mode is disabled within a session with SET autocommit = 0, the session always has a transaction open. ACOMMIT or ROLLBACK statement ends the current transaction and a new one starts.
如果一个会话使用了 SET autocommit = 0语句禁用了autocommit模式,则会话总是打开事务,一个COMMIT或者ROLLBACK 语句结束当前事务并启动一个新事务
If a session that has autocommit disabled ends without explicitly committing the final transaction, MySQL rolls back that transaction.
如果一个会话明确激禁用了 autocommit模式,并且最后也没有显式的提交最终事务,那么mysql将会对这个事务进行回滚
Some statements implicitly end a transaction, as if you had done a COMMIT before executing the statement. For details, see Section 13.3.3, “Statements That Cause an Implicit Commit”.
有一些语句会隐式的结束事务,例如你在执行语句前先执行一个COMMIT。更多信息,请查看 Section 13.3.3, “Statements That Cause an Implicit Commit”.
A COMMIT means that the changes made in the current transaction are made permanent and become visible to other sessions. A ROLLBACK statement, on the other hand, cancels all modifications made by the current transaction. Both COMMITand ROLLBACK release all InnoDB locks that were set during the current transaction.
一个COMMIT意味着当前事务中所做的更改是永久的,并且可以在其他会话中看到。另一方面,ROLLBACK语句取消了当前事务所做的所有修改。COMMIT和ROLLBACK 都释放了在当前事务中设置的所有InnoDB锁。
Grouping DML Operations with Transactions
事务的DML操作分组
By default, connection to the MySQL server begins with autocommit mode enabled, which automatically commits every SQL statement as you execute it. This mode of operation might be unfamiliar if you have experience with other database systems, where it is standard practice to issue a sequence of DML statements and commit them or roll them back all together.
默认情况下,与MySQL服务器的连接始于启用 autocommit 模式,该模式会在您执行时自动提交每个SQL语句,如果您有其他数据库系统的经验,这种操作模式可能并不熟悉,标准做法是发布一系列DML语句并提交它们或将它们一起回滚。
To use multiple-statement transactions, switch autocommit off with the SQL statement SET autocommit = 0 and end each transaction with COMMIT or ROLLBACK as appropriate. To leave autocommit on, begin each transaction with START TRANSACTION and end it with COMMIT or ROLLBACK. The following example shows two transactions. The first is committed; the second is rolled back.
使用多语句事务,使用SQL语句SET autocommit = 0关闭自动提交,并根据需要使用COMMIT或ROLLBACK结束每个事务,离开了自动提交,用START TRANSACTION开始每个事务并用COMMIT或ROLLBACK结束它.下面的例子显示了两个事务,第一个提交了,第二个回滚了
shell> mysql test
mysql> CREATE TABLE customer (a INT, b CHAR (20), INDEX (a));
Query OK, 0 rows affected (0.00 sec)
mysql> -- Do a transaction with autocommit turned on.
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO customer VALUES (10, 'Heikki');
Query OK, 1 row affected (0.00 sec)
mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)
mysql> -- Do another transaction with autocommit turned off.
mysql> SET autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO customer VALUES (15, 'John');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO customer VALUES (20, 'Paul');
Query OK, 1 row affected (0.00 sec)
mysql> DELETE FROM customer WHERE b = 'Heikki';
Query OK, 1 row affected (0.00 sec)
mysql> -- Now we undo those last 2 inserts and the delete.
mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM customer;
+------+--------+
| a | b |
+------+--------+
| 10 | Heikki |
+------+--------+
1 row in set (0.00 sec)
mysql>
Transactions in Client-Side Languages
客户端语言中的事务
In APIs such as PHP, Perl DBI, JDBC, ODBC, or the standard C call interface of MySQL, you can send transaction control statements such as COMMIT to the MySQL server as strings just like any other SQL statements such as SELECT or INSERT. Some APIs also offer separate special transaction commit and rollback functions or methods.
API 比如PHP,PERL,JBDC,ODBC,或者其他MySQL的标准C调用接口,你可以发送事务控制语句例如COMMIT语句给mysql服务器,就像其他的SQL语句一样,例如SELECT或INSERT,一些API还提供了单独的特殊事务提交和回滚函数或方法
标签:14.5,autocommit,transaction,ROLLBACK,mysql,语句,COMMIT,2.2 来源: https://blog.51cto.com/itzhoujun/2354193