summaryrefslogtreecommitdiff
path: root/mysql-test/t/innodb-lock.test
blob: 05df36158222a8740af301384ce983c4df555fd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
-- source include/have_innodb.inc

#
# Check and select innodb lock type
#

set global innodb_table_locks=1;

select @@innodb_table_locks;

#
# Testing of explicit table locks with enforced table locks
#

connect (con1,localhost,root,,);
connect (con2,localhost,root,,);

--disable_warnings
drop table if exists t1;
--enable_warnings

#
# Testing of explicit table locks with enforced table locks
#

set @@innodb_table_locks=1;

connection con1;
create table t1 (id integer, x integer) engine=INNODB;
insert into t1 values(0, 0);
set autocommit=0;
SELECT * from t1 where id = 0 FOR UPDATE;

connection con2;
set autocommit=0;

# The following statement should hang because con1 is locking the page
--send
lock table t1 write;
--sleep 2

connection con1;
update t1 set x=1 where id = 0;
select * from t1;
commit;

connection con2;
reap;
update t1 set x=2 where id = 0;
commit;
unlock tables;

connection con1;
select * from t1;
commit;

drop table t1;

--echo #
--echo # Old lock method (where LOCK TABLE was ignored by InnoDB) no longer
--echo # works when LOCK TABLE ... WRITE is used due to fix for bugs #46272
--echo # "MySQL 5.4.4, new MDL: unnecessary and bug #37346 "innodb does not
--echo # detect deadlock between update and alter table". But it still works
--echo # for LOCK TABLE ... READ.
--echo #

set @@innodb_table_locks=0;

create table t1 (id integer primary key, x integer) engine=INNODB;
insert into t1 values(0, 0),(1,1),(2,2);
commit;
SELECT * from t1 where id = 0 FOR UPDATE;

--echo # Connection 'con2'.
connection con2;
set autocommit=0;
set @@innodb_table_locks=0;

--echo # The following statement should block because SQL-level lock
--echo # is taken on t1 which will wait until concurrent transaction
--echo # is commited.
--echo # Sending:
--send lock table t1 write;

--echo # Connection 'con1'.
connection con1;
--echo # Wait until LOCK TABLE is blocked on SQL-level lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # We should be able to do UPDATEs and SELECTs within transaction.
update t1 set x=1 where id = 0;
select * from t1;
--echo # Unblock LOCK TABLE.
commit;

--echo # Connection 'con2'.
connection con2;
--echo # Reap LOCK TABLE.
--reap
unlock tables;

--echo # Connection 'con1'.
connection con1;

select * from t1 where id = 0 for update;

--echo # Connection 'con2'.
connection con2;
--echo # The below statement should not be blocked as LOCK TABLES ... READ
--echo # does not take strong SQL-level lock on t1. SELECTs which do not
--echo # conflict with transaction in the first connections should not be
--echo # blocked.
lock table t1 read;
select * from t1;
select * from t1 where id = 1 lock in share mode;
unlock tables;
select * from t1;
commit;

--echo # Connection 'con1'.
connection con1;
commit;

drop table t1;

# End of 4.1 tests