環境:Server version: 5.6.21-log MySQL Community Server (GPL)
前提提要:
innodb_rollback_on_timeout是啥作用?
答:事務B在鎖等待超時后是回滾事務內所有的statement還是最后一條語句;
0表示rollback最后一條語句,默認值;有點坑
1表示回滾事務B內所有的statements;
此參數是只讀參數,需在my.cnf中配置,並且重啟生效;
注意:回滾statements后不自動commit或rollback事務;坑
表結構:
mysql> show create table t12\G; *************************** 1. row *************************** Table: t12 Create Table: CREATE TABLE `t12` ( `a` int(10) unsigned NOT NULL AUTO_INCREMENT, `b` varchar(766) DEFAULT NULL, `c` int(11) DEFAULT NULL, PRIMARY KEY (`a`), KEY `b` (`b`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1 1 row in set (0.00 sec)
實驗一:
參數配置: mysql> show variables like 'innodb_rollback_on_timeout'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_rollback_on_timeout | ON | +----------------------------+-------+ 1 row in set (0.00 sec) mysql> show variables like 'tx_iso%'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | tx_isolation | REPEATABLE-READ | +---------------+-----------------+ 1 row in set (0.00 sec) mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | ON | +---------------+-------+ 1 row in set (0.00 sec)
過程:
實驗二:
參數配置: mysql> show variables like 'innodb_rollback_on_timeout'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_rollback_on_timeout | OFF | +----------------------------+-------+ 1 row in set (0.00 sec) mysql> show variables like 'tx_iso%'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | tx_isolation | REPEATABLE-READ | +---------------+-----------------+ 1 row in set (0.00 sec) mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | ON | +---------------+-------+ 1 row in set (0.00 sec)
過程:
總結:
1、關閉innodb_rollback_on_timeout后,一旦以begin;start transaction;等語句開啟一個事務,當鎖等待超時后,該事務請求的鎖將不釋放,直到事務提交或回滾或會話超時;
所以autocommit參數建議設置成ON,只要程序沒有顯示開啟事務,就可以避免上述鎖未釋放問題。
2、開啟innodb_rollback_on_timeout后,一旦鎖等待超時,是事務內sql將全部回滾,且釋放之前請求的鎖。
3、當autocommit=on,只要不顯示開啟事務,將不存在上面2個問題,即鎖的問題和回滾的問題。