autocommit 是什么
“ 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.
大致意思是:在InooDB中,所有用户活动都发生在事务内部,如果autocommit
状态是开启的,每一个SQL都会作为一个事务提交。
autocommit 设置
mysql 默认是开启 autocommit
的,但是代码中开始事务的后单个为什么没有提交哪,我们来看看手册上是怎么说的:
“ With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK. The autocommit mode then reverts to its previous state. ”
大致的意思是:autocommit
遇到 START TRANSACTION 会置为关闭状态,在提交或回滚后,恢复为原来的值。
这就解释了为什么 autocommit
是开启的,事务开启后执行的 SQL 不是单个提交的。
应用程序连接中的 autocommit
autocommit
真正状态要看应用程序框架(如连接池的库)中的设置,说白了,就是得到数据库连接以后,显示的调用一次 set autocommit ON/OFF。