简介
如果线程1对某条数据加排他锁后并不释放,或者未及时释放,另外一个线程2要对该数据加排他锁,就需要等待。但是不会无线等待下去,默认等待50秒,超时后会报错。
超时后的报错信息:
Lock wait timeout exceeded; try restarting transaction
超时时间可以通过 innodb_lock_wait_timeout 变量控制。
查询值:
select @@innodb_lock_wait_timeout
设置值,比如设置为2秒:
set @@innodb_lock_wait_timeout=2
示例
建表:
create table test_table (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
name varchar(32) not null,
PRIMARY KEY (`id`),
UNIQUE KEY uk_name(`name`)
) engine = InnoDB character set = utf8mb4;
插入数据:
insert into test_table(name) values('aaa');
线程1加锁不释放:
mysql> start transaction;
mysql> select * from test_table where id=1 for update
+----+------+
| id | name |
+----+------+
| 1 | aaa |
+----+------+
线程2加锁超时报错:
mysql> start transaction;
mysql> select * from test_table where id=1 for update
(1205, u'Lock wait timeout exceeded; try restarting transaction')