MySQL innodb_rollback_on_timeout參數對鎖的影響

環境:Server version:         5.6.21-log MySQL Community Server (GPL)mysql

前提提要:sql

innodb_rollback_on_timeout是啥做用?
 答:事務B在鎖等待超時後是回滾事務內全部的statement仍是最後一條語句;
      0表示rollback最後一條語句,默認值;有點坑
      1表示回滾事務B內全部的statements;
 此參數是隻讀參數,需在my.cnf中配置,而且重啓生效;
 注意:回滾statements後不自動commit或rollback事務;坑

表結構:blog

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)

 過程:it

 

 

實驗二:io

參數配置:
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)

過程:innodb

 

 總結:table

一、關閉innodb_rollback_on_timeout後,一旦以begin;start transaction;等語句開啓一個事務,當鎖等待超時後,該事務請求的鎖將不釋放,直到事務提交或回滾或會話超時;配置

因此autocommit參數建議設置成ON,只要程序沒有顯示開啓事務,就能夠避免上述鎖未釋放問題。請求

二、開啓innodb_rollback_on_timeout後,一旦鎖等待超時,是事務內sql將所有回滾,且釋放以前請求的鎖。

三、當autocommit=on,只要不顯示開啓事務,將不存在上面2個問題,即鎖的問題和回滾的問題。

相關文章
相關標籤/搜索