summaryrefslogtreecommitdiffstats
path: root/mysql-test/suite/storage_engine/lock.test
blob: 2ee77ad93f3a8e731dfe681c99a879805a09378e (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Standard table locking:
#   LOCK TABLE .. READ 
#   LOCK TABLE .. [LOW_PRIORITY] WRITE 
#   UNLOCK TABLES
#
# and global locking:
# FLUSH TABLES [..] WITH READ LOCK
# 
#   
--source have_engine.inc

# Save the initial number of concurrent sessions.
--source include/count_sessions.inc

connect (con1,localhost,root,,);
SET lock_wait_timeout=1;

connection default;

--disable_warnings
DROP TABLE IF EXISTS t1, t2, t3;
--enable_warnings

--let $create_definition = id $int_col, id2 $int_col
--source create_table.inc

INSERT INTO t1 (id,id2) VALUES (1,1),(1,2),(1,3);

# LOW_PRIORITY has no effect, but is still syntactically correct
LOCK TABLE t1 LOW_PRIORITY WRITE;
SELECT id2,COUNT(DISTINCT id) FROM t1 GROUP BY id2;

UPDATE t1 SET id=-1 WHERE id=1;
if ($mysql_errname)
{
  --let $functionality = UPDATE
  --source unexpected_result.inc
}

connection con1;
# With WRITE lock held by connection 'default',
# nobody else can access the table
--let $error_codes = ER_LOCK_WAIT_TIMEOUT
SELECT id,id2 FROM t1;
--source check_errors.inc
--let $error_codes = ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t1 READ;
--source check_errors.inc

connection default;
LOCK TABLE t1 READ;
--let $error_codes = ER_TABLE_NOT_LOCKED_FOR_WRITE
UPDATE t1 SET id=1 WHERE id=1;
--source check_errors.inc
if ($mysql_errname != ER_TABLE_NOT_LOCKED_FOR_WRITE)
{
  --let $functonality = UPDATE or locking
  --source unexpected_result.inc
}

connection con1;
# With READ lock held by connection 'default',
# it should be possible to read from the table
# or acquire another READ lock, 
# but not update it or acquire WRITE lock
SELECT COUNT(DISTINCT id) FROM t1;
--let $error_codes = ER_LOCK_WAIT_TIMEOUT
UPDATE t1 SET id=2 WHERE id=2;
--source check_errors.inc
--let $error_codes = ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t1 WRITE;
--source check_errors.inc
LOCK TABLE t1 READ;
UNLOCK TABLES;


--connection default

--let $error_codes = ER_TABLE_NOT_LOCKED
--let $table_name = t2
--source create_table.inc

--let $table_name = t2
--let $temporary = 1
--source create_table.inc

DROP TABLE IF EXISTS t2;

UNLOCK TABLES;

--let $table_name = t2
--let $create_definition = id $int_col, id2 $int_col
--source create_table.inc
LOCK TABLE t1 WRITE, t2 WRITE;
INSERT INTO t2 (id,id2) SELECT id,id2 FROM t1;
UPDATE t1 SET id=1 WHERE id=-1;
if ($mysql_errname)
{
  --let $functionality = UPDATE
  --source unexpected_result.inc
}
DROP TABLE t1,t2;

#
# INSERT ... SELECT with lock tables
#

--let $create_definition = i1 $int_col, nr $int_col
--source create_table.inc

--let $table_name = t2
--let $create_definition = nr $int_col, nm $int_col
--source create_table.inc

INSERT INTO t2 (nr,nm) VALUES (1,3);
INSERT INTO t2 (nr,nm) VALUES (2,4);

lock tables t1 write, t2 read;
INSERT INTO t1 (i1,nr) SELECT 1, nr FROM t2 WHERE nm=3;
INSERT INTO t1 (i1,nr) SELECT 2, nr FROM t2 WHERE nm=4;
UNLOCK TABLES;

LOCK TABLES t1 WRITE;
--let $error_codes = ER_TABLE_NOT_LOCKED
INSERT INTO t1 (i1,nr) SELECT i1, nr FROM t1;
--source check_errors.inc
UNLOCK TABLES;
LOCK TABLES t1 WRITE, t1 AS t1_alias READ;
INSERT INTO t1 (i1,nr) SELECT i1, nr FROM t1 AS t1_alias;
--let $error_codes = ER_TABLE_NOT_LOCKED
DROP TABLE t1,t2;
--source check_errors.inc
UNLOCK TABLES;
DROP TABLE t1,t2;

#
# Check that a dropped table is removed from a lock

--source create_table.inc
--let $table_name = t2
--source create_table.inc
--let $table_name = t3
--source create_table.inc
LOCK TABLES t1 WRITE, t2 WRITE, t3 WRITE;
# This removes one table after the other from the lock.
DROP TABLE t2, t3, t1;
#
# Check that a lock merge works
--source create_table.inc
--let $table_name = t2
--source create_table.inc
--let $table_name = t3
--source create_table.inc
LOCK TABLES t1 WRITE, t2 WRITE, t3 WRITE, t1 AS t4 READ;

--let $alter_definition = ADD COLUMN c2 $int_col
--let $table_name = t2
--source alter_table.inc
if ($mysql_errname)
{
  --let $my_last_stmt = $alter_statement
  --let $functionality = ALTER TABLE
  --source unexpected_result.inc
}

DROP TABLE t1, t2, t3;

# FLUSH TABLES is not permitted when there is an active LOCK TABLES .. READ,
# FLUSH TABLES .. WITH READ LOCK should be used instead
# (and for other connections the table is locked)

--source create_table.inc
--let $table_name = t2
--source create_table.inc

LOCK TABLE t1 READ, t2 READ;
--let $error_codes = ER_TABLE_NOT_LOCKED_FOR_WRITE
FLUSH TABLE t1;
--source check_errors.inc
--let $error_codes = ER_TABLE_NOT_LOCKED_FOR_WRITE
FLUSH TABLES;
--source check_errors.inc
--let $error_codes = ER_LOCK_OR_ACTIVE_TRANSACTION
FLUSH TABLES t1, t2 WITH READ LOCK;
--source check_errors.inc
UNLOCK TABLES;

FLUSH TABLES t1, t2 WITH READ LOCK;

--connection con1
--let $error_codes = ER_LOCK_WAIT_TIMEOUT
INSERT INTO t1 (a,b) VALUES (1,'a'),(2,'b');
--source check_errors.inc

--connection default
UNLOCK TABLES;

# Global lock

FLUSH TABLES WITH READ LOCK;

--connection con1
--let $error_codes = ER_LOCK_WAIT_TIMEOUT
INSERT INTO t1 (a,b) VALUES (1,'a'),(2,'b');
--source check_errors.inc

--connection default
UNLOCK TABLES;
INSERT INTO t1 (a,b) VALUES (1,'a'),(2,'b');
FLUSH TABLES WITH READ LOCK AND DISABLE CHECKPOINT;
--let $error_codes = ER_CANT_UPDATE_WITH_READLOCK
DROP TABLE t1, t2;
--source check_errors.inc
UNLOCK TABLES;
DROP TABLE t1, t2;

--disconnect con1

#
# Ensure that FLUSH TABLES doesn't substitute a base locked table
# with a temporary one.
#

--source create_table.inc
--let $table_name = t2
--source create_table.inc
LOCK TABLE t1 WRITE, t2 WRITE;

--let $temporary = 1
--source create_table.inc 
FLUSH TABLE t1;
DROP TEMPORARY TABLE t1;
SELECT a,b FROM t1;
UNLOCK TABLES;
DROP TABLE t1, t2;

# Check that all connections opened by test cases in this file are really
# gone so execution of other tests won't be affected by their presence.
--source include/wait_until_count_sessions.inc

--source cleanup_engine.inc