~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
DROP DATABASE  IF EXISTS mysqltest;
2
DROP TABLE     IF EXISTS t1, t2, t3, t4, v1;
3
DROP VIEW      IF EXISTS t1, t2, t3, t4, v1;
4
DROP PROCEDURE IF EXISTS lock_t1_excl;
5
DROP PROCEDURE IF EXISTS count_t2;
6
DROP PROCEDURE IF EXISTS update_t2;
7
DROP TRIGGER   IF EXISTS t1_ai;
8
#
9
# WL3561 - transactional LOCK TABLE - Syntax tests
10
# ================================================
11
CREATE TABLE t1 (c1 INT ) ENGINE=InnoDB;
12
CREATE TABLE t2 (c2 INT ) ENGINE=InnoDB;
13
CREATE TABLE t3 (c3 INT ) ENGINE=InnoDB;
14
#
15
# Valid syntax for non-transactional locks.
16
LOCK TABLE t1 READ, t2 WRITE;
17
UNLOCK TABLES;
18
LOCK TABLE t1 READ LOCAL, t2 LOW_PRIORITY WRITE;
19
UNLOCK TABLES;
20
#
21
# Valid syntax for transactional locks.
22
LOCK TABLE t1 IN SHARE MODE, t2 IN EXCLUSIVE MODE;
23
UNLOCK TABLES;
24
#
25
# Valid syntax for transactional locks with NOWAIT option.
26
## In the preliminary reference implementation we expect these errors:
27
## NOWAIT+SHARE="timed out", NOWAIT+EXCLUSIVE="not supported".
28
## Statements abort on first error.
29
LOCK TABLE t1 IN SHARE MODE NOWAIT, t2 IN EXCLUSIVE MODE NOWAIT;
30
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
31
LOCK TABLE t1 IN EXCLUSIVE MODE NOWAIT, t2 IN SHARE MODE NOWAIT;
32
ERROR 42000: Table 't1' uses an extension that doesn't exist in this MySQL version
33
LOCK TABLE t1 IN EXCLUSIVE MODE, t2 IN SHARE MODE NOWAIT;
34
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
35
#
36
# Valid syntax for aliases with and without 'AS'.
37
LOCK TABLE t1 AS a1 READ, t2 a2 WRITE;
38
UNLOCK TABLES;
39
LOCK TABLE t1 AS a1 IN SHARE MODE, t2 a2 IN EXCLUSIVE MODE;
40
UNLOCK TABLES;
41
#
42
# Transactional locks taken on a view.
43
CREATE VIEW v1 AS SELECT * FROM t1, t2 WHERE t1.c1 = t2.c2;
44
LOCK TABLE v1 IN SHARE MODE;
45
LOCK TABLE v1 IN EXCLUSIVE MODE;
46
DROP VIEW v1;
47
#
48
# Locking INFORMATION_SCHEMA fails on missing privileges.
49
LOCK TABLE information_schema.tables IN SHARE MODE;
50
ERROR 42000: Access denied for user 'root'@'localhost' to database 'information_schema'
51
LOCK TABLE information_schema.tables IN EXCLUSIVE MODE;
52
ERROR 42000: Access denied for user 'root'@'localhost' to database 'information_schema'
53
LOCK TABLE information_schema.tables READ;
54
ERROR 42000: Access denied for user 'root'@'localhost' to database 'information_schema'
55
LOCK TABLE information_schema.tables WRITE;
56
ERROR 42000: Access denied for user 'root'@'localhost' to database 'information_schema'
57
#
58
# The new keywords EXCLUSIVE and NOWAIT are not reserved words.
59
CREATE TABLE t4 (exclusive INT, nowait INT) ENGINE=InnoDB;
60
LOCK TABLE t4 WRITE;
61
DROP TABLE t4;
62
#
63
# Syntax errors for misspelled modes or left out symbols.
64
##-------------------------------------------------------
65
LOCK TABLE t1 IN SHARED MODE;
66
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SHARED MODE' at line 1
67
LOCK TABLE t1 SHARE MODE;
68
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MODE' at line 1
69
LOCK TABLE t1 IN SHARE;
70
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
71
LOCK TABLE t1 IN MODE;
72
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MODE' at line 1
73
LOCK TABLE t1 READ NOWAIT, t2 WRITE NOWAIT;
74
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOWAIT, t2 WRITE NOWAIT' at line 1
75
LOCK TABLE t1 READ NOWAIT, t2 IN EXCLUSIVE MODE NOWAIT;
76
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOWAIT, t2 IN EXCLUSIVE MODE NOWAIT' at line 1
77
LOCK TABLE t1 IN SHARE MODE NOWAIT, t2 WRITE NOWAIT;
78
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOWAIT' at line 1
79
LOCK TABLE t1 IN SHARED MODE NOWAIT;
80
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SHARED MODE NOWAIT' at line 1
81
LOCK TABLE t1 SHARE MODE NOWAIT;
82
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MODE NOWAIT' at line 1
83
LOCK TABLE t1 IN SHARE NOWAIT;
84
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOWAIT' at line 1
85
LOCK TABLE t1 IN MODE NOWAIT;
86
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MODE NOWAIT' at line 1
87
##----------------------
88
## End of syntax errors.
89
#
90
#
91
# WL3561 - transactional LOCK TABLE - Lock method conversion
92
# ==========================================================
93
#
94
# Implicit lock method conversion due to mix in statement.
95
LOCK TABLE t1 READ, t2 IN EXCLUSIVE MODE;
96
Warnings:
97
Warning	1613	Converted to non-transactional lock on 't2'
98
UNLOCK TABLES;
99
# Lock t1 share (converted to read), t2 write.
100
LOCK TABLE t1 IN SHARE MODE, t2 WRITE;
101
Warnings:
102
Warning	1613	Converted to non-transactional lock on 't1'
103
# Show t1 is read locked, t2 write locked.
104
INSERT INTO t1 SELECT * FROM t2;
105
ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
106
INSERT INTO t2 SELECT * FROM t1;
107
#
108
# Implicit lock method conversion due to existing non-transact. locks.
109
# Implicitly unlock existing non-transactional locks and take new ones.
110
# Lock t1 exclusive (converted to write), t2 share (converted to read).
111
LOCK TABLE t1 IN EXCLUSIVE MODE, t2 IN SHARE MODE;
112
Warnings:
113
Warning	1613	Converted to non-transactional lock on 't1'
114
Warning	1613	Converted to non-transactional lock on 't2'
115
# Show t1 is write locked, t2 read locked.
116
INSERT INTO t1 SELECT * FROM t2;
117
INSERT INTO t2 SELECT * FROM t1;
118
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
119
UNLOCK TABLES;
120
#
121
# Reject lock method conversion in strict mode.
122
# Set strict mode.
123
SET @wl3561_save_sql_mode= @@SQL_MODE;
124
SET @@SQL_MODE= 'STRICT_ALL_TABLES';
125
# Try mixed mode locks.
126
LOCK TABLE t1 READ, t2 IN EXCLUSIVE MODE;
127
ERROR HY000: Cannot convert to non-transactional lock in strict mode on 't2'
128
LOCK TABLE t1 IN SHARE MODE, t2 WRITE;
129
ERROR HY000: Cannot convert to non-transactional lock in strict mode on 't1'
130
# Lock non-transactional.
131
LOCK TABLE t1 READ, t2 WRITE;
132
# Try transactional locks on top of the existing non-transactional locks.
133
LOCK TABLE t1 IN SHARE MODE, t2 IN EXCLUSIVE MODE;
134
ERROR HY000: Cannot convert to non-transactional lock in strict mode on 't1'
135
## Error is reported on first table only. Show both errors:
136
SHOW WARNINGS;
137
Level	Code	Message
138
Error	1614	Cannot convert to non-transactional lock in strict mode on 't1'
139
Error	1614	Cannot convert to non-transactional lock in strict mode on 't2'
140
UNLOCK TABLES;
141
SET @@SQL_MODE= @wl3561_save_sql_mode;
142
#
143
# Reject lock method conversion in an active transaction.
144
# Start transaction.
145
START TRANSACTION;
146
# Try mixed mode locks.
147
LOCK TABLE t1 READ, t2 IN EXCLUSIVE MODE;
148
ERROR HY000: Cannot convert to non-transactional lock in an active transaction on 't2'
149
LOCK TABLE t1 IN SHARE MODE, t2 WRITE;
150
ERROR HY000: Cannot convert to non-transactional lock in an active transaction on 't1'
151
COMMIT;
152
#
153
# Implicit lock method conversion for non-transactional storage engine.
154
# Create a non-transactional table.
155
CREATE TABLE t4 (c4 INT) ENGINE= MyISAM;
156
# Request a transactional lock, which is converted to non-transactional.
157
LOCK TABLE t4 IN SHARE MODE;
158
Warnings:
159
Warning	1613	Converted to non-transactional lock on 't4'
160
# Try a conflict with the existing non-transactional lock.
161
INSERT INTO t4 VALUES(444);
162
ERROR HY000: Table 't4' was locked with a READ lock and can't be updated
163
UNLOCK TABLES;
164
# Set strict mode.
165
SET @@SQL_MODE= 'STRICT_ALL_TABLES';
166
# Try a transactional lock, which would need a conversion.
167
LOCK TABLE t4 IN SHARE MODE;
168
ERROR HY000: Cannot convert to non-transactional lock in strict mode on 't4'
169
SET @@SQL_MODE= @wl3561_save_sql_mode;
170
#
171
# View with transactional and non-transactional storage engine.
172
CREATE VIEW v1 AS SELECT * FROM t3, t4 WHERE t3.c3 = t4.c4;
173
# Request a share lock on the view, which is converted to read locks.
174
LOCK TABLE v1 IN SHARE MODE;
175
Warnings:
176
Warning	1613	Converted to non-transactional lock on 't3'
177
Warning	1613	Converted to non-transactional lock on 't4'
178
# Show that read locks on the base tables prohibit writing ...
179
INSERT INTO t3 SELECT * FROM t4;
180
ERROR HY000: Table 't3' was locked with a READ lock and can't be updated
181
INSERT INTO t4 SELECT * FROM t3;
182
ERROR HY000: Table 't4' was locked with a READ lock and can't be updated
183
# ... but allow reading.
184
SELECT COUNT(*) FROM t3, t4 WHERE t3.c3 = t4.c4;
185
COUNT(*)
186
0
187
SELECT COUNT(*) FROM v1;
188
COUNT(*)
189
0
190
## Report conversion on view due to existing non-transactional locks.
191
LOCK TABLE v1 IN EXCLUSIVE MODE;
192
Warnings:
193
Warning	1613	Converted to non-transactional lock on 'v1'
194
INSERT INTO t3 VALUES(333);
195
INSERT INTO t4 VALUES(444);
196
INSERT INTO t1 VALUES(111);
197
ERROR HY000: Table 't1' was not locked with LOCK TABLES
198
UNLOCK TABLES;
199
## Now report conversion on base table again.
200
LOCK TABLE v1 IN EXCLUSIVE MODE;
201
Warnings:
202
Warning	1613	Converted to non-transactional lock on 't3'
203
Warning	1613	Converted to non-transactional lock on 't4'
204
INSERT INTO t3 VALUES(333);
205
INSERT INTO t4 VALUES(444);
206
INSERT INTO t1 VALUES(111);
207
ERROR HY000: Table 't1' was not locked with LOCK TABLES
208
UNLOCK TABLES;
209
DROP VIEW v1;
210
TRUNCATE t4;
211
#
212
# Insufficient privileges do not unlock tables nor end transactions.
213
# Prepare database, tables and an user with insufficient privileges.
214
# Make a new connection with this user.
215
CREATE DATABASE mysqltest;
216
CREATE TABLE mysqltest.t5 (c5 INT) ENGINE=InnoDB;
217
CREATE TABLE mysqltest.t6 (c6 INT) ENGINE=InnoDB;
218
CREATE USER mysqltest_1@localhost;
219
GRANT SELECT, INSERT ON mysqltest.* TO mysqltest_1@localhost;
220
# connection conn1.
221
# Show sufficient privileges to lock tables in the test database.
222
LOCK TABLE t1 READ, t2 WRITE;
223
# Show insufficient privileges in the mysqltest database.
224
LOCK TABLE mysqltest.t5 READ, mysqltest.t6 WRITE;
225
ERROR 42000: Access denied for user 'mysqltest_1'@'localhost' to database 'mysqltest'
226
# Show that the locks in 'test' still exist.
227
INSERT INTO t1 SELECT * FROM t2;
228
ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
229
INSERT INTO t2 SELECT * FROM t1;
230
# Unlock tables.
231
UNLOCK TABLES;
232
# Start transaction.
233
START TRANSACTION;
234
# Insert a value.
235
INSERT INTO t1 VALUES(111);
236
# Try a lock that fails on privileges.
237
LOCK TABLE mysqltest.t5 READ;
238
ERROR 42000: Access denied for user 'mysqltest_1'@'localhost' to database 'mysqltest'
239
# Rollback transaction.
240
ROLLBACK;
241
# Show that the inserted value has gone.
242
SELECT * FROM t1;
243
c1
244
# Drop the connection with the unprivileged user.
245
# connection default.
246
#
247
# Sufficient privileges do unlock tables and end transactions.
248
# Grant sufficient privileges to the user.
249
# Make a new connection with this user.
250
GRANT SELECT, INSERT, LOCK TABLES ON mysqltest.* TO mysqltest_1@localhost;
251
# connection conn1.
252
# Lock tables in the test database.
253
LOCK TABLE t1 READ, t2 WRITE;
254
# Lock tables in the mysqltest database.
255
LOCK TABLE mysqltest.t5 READ, mysqltest.t6 WRITE;
256
# Show that the locks in 'test' have been replaced ...
257
INSERT INTO t1 SELECT * FROM t2;
258
ERROR HY000: Table 't1' was not locked with LOCK TABLES
259
INSERT INTO t2 SELECT * FROM t1;
260
ERROR HY000: Table 't2' was not locked with LOCK TABLES
261
# ... by the locks in 'mysqltest'.
262
INSERT INTO mysqltest.t5 SELECT * FROM mysqltest.t6;
263
ERROR HY000: Table 't5' was locked with a READ lock and can't be updated
264
INSERT INTO mysqltest.t6 SELECT * FROM mysqltest.t5;
265
# Unlock tables.
266
UNLOCK TABLES;
267
# Start transaction.
268
START TRANSACTION;
269
# Insert a value.
270
INSERT INTO t1 VALUES(111);
271
# Take a new lock.
272
LOCK TABLE mysqltest.t5 READ;
273
# Rollback transaction.
274
ROLLBACK;
275
UNLOCK TABLES;
276
# Show that the inserted value had been committed.
277
SELECT * FROM t1;
278
c1
279
111
280
TRUNCATE t1;
281
# connection default.
282
UNLOCK TABLES;
283
DROP USER mysqltest_1@localhost;
284
DROP DATABASE mysqltest;
285
DROP TABLE t1, t2, t3, t4;
286
#
287
# WL3594 - transactional LOCK TABLE Testing - Functional tests
288
# ============================================================
289
# Prepare tables and connections.
290
# Set AUTOCOMMIT= 0 in each connection.
291
SET AUTOCOMMIT= 0;
292
CREATE TABLE t1 (c1 INT) ENGINE=InnoDB;
293
CREATE TABLE t2 (c2 INT) ENGINE=InnoDB;
294
# connection conn1.
295
SET AUTOCOMMIT= 0;
296
# connection conn2.
297
SET AUTOCOMMIT= 0;
298
# connection default.
299
#
300
# Transactional lock behaviour:
301
# LOCK TABLE does _not_ commit a transaction.
302
# Insert a value.
303
INSERT INTO t1 VALUES (111);
304
# Lock transactional.
305
LOCK TABLE t1 IN EXCLUSIVE MODE;
306
# Rollback.
307
ROLLBACK;
308
# Show that the inserted value has gone.
309
SELECT * FROM t1;
310
c1
311
#
312
# After LOCK TABLE one can access tables not mentioned in LOCK TABLE.
313
# Lock t1 transactional.
314
LOCK TABLE t1 IN EXCLUSIVE MODE;
315
# Insert a value into t2.
316
INSERT INTO t2 VALUES (222);
317
# Show that the inserted value is indeed in the table.
318
SELECT * FROM t2;
319
c2
320
222
321
#
322
# One can issue LOCK TABLE many times, adding more tables.
323
# Lock t2 transactional.
324
LOCK TABLE t2 IN EXCLUSIVE MODE;
325
#
326
# LOCK TABLE does not rollback a transaction.
327
# Show that the inserted value is still in the table.
328
SELECT * FROM t2;
329
c2
330
222
331
# Rollback transaction.
332
ROLLBACK;
333
# Show that the inserted value has gone.
334
SELECT * FROM t2;
335
c2
336
#
337
# Tables are unlocked at the end of transaction (commit).
338
# Take an exclusive lock.
339
LOCK TABLE t1 IN EXCLUSIVE MODE;
340
# Commit.
341
COMMIT;
342
# connection conn1.
343
# Take an exclusive lock.
344
# This would fail after timeout if t1 is still locked.
345
LOCK TABLE t1 IN EXCLUSIVE MODE;
346
# Commit.
347
COMMIT;
348
# connection default.
349
#
350
# Tables are unlocked at the end of transaction (rollback).
351
# Take an exclusive lock.
352
LOCK TABLE t1 IN EXCLUSIVE MODE;
353
# Rollback.
354
ROLLBACK;
355
# connection conn1.
356
# Take an exclusive lock.
357
# This would fail after timeout if t1 is still locked.
358
LOCK TABLE t1 IN EXCLUSIVE MODE;
359
# Rollback.
360
ROLLBACK;
361
# connection default.
362
#
363
# UNLOCK TABLES does not touch a transaction when
364
# no non-transactional table locks exist.
365
# Take a transactional lock.
366
LOCK TABLE t1 IN EXCLUSIVE MODE;
367
# Insert a value.
368
INSERT INTO t1 VALUES(111);
369
# Unlock (non-transactional) table locks.
370
UNLOCK TABLES;
371
# Show that the inserted value is still in the table.
372
SELECT * FROM t1;
373
c1
374
111
375
# Rollback.
376
ROLLBACK;
377
# Show that the inserted value has gone.
378
SELECT * FROM t1;
379
c1
380
#
381
# UNLOCK TABLES commits a transaction when
382
# non-transactional table locks exist.
383
# Take a non-transactional lock.
384
LOCK TABLE t1 WRITE;
385
# Insert a value.
386
INSERT INTO t1 VALUES(111);
387
# Unlock (non-transactional) table locks.
388
UNLOCK TABLES;
389
# Show that the inserted value is still in the table.
390
SELECT * FROM t1;
391
c1
392
111
393
# Rollback.
394
ROLLBACK;
395
# Show that the inserted value is still in the table.
396
SELECT * FROM t1;
397
c1
398
111
399
TRUNCATE t1;
400
#
401
# START TRANSACTION removes a previous lock.
402
# Take an exclusive lock.
403
LOCK TABLE t1 IN EXCLUSIVE MODE;
404
# Start transaction.
405
START TRANSACTION;
406
# connection conn1.
407
# Take an exclusive lock.
408
# This would fail after timeout if t1 is still locked.
409
LOCK TABLE t1 IN EXCLUSIVE MODE;
410
COMMIT;
411
# connection default.
412
COMMIT;
413
#
414
# With Auto commit on, transactional locks will be ignored
415
# Set AUTOCOMMIT= 1.
416
SET AUTOCOMMIT= 1;
417
# Take an exclusive lock.
418
LOCK TABLE t1 IN EXCLUSIVE MODE;
419
# connection conn1.
420
# Set AUTOCOMMIT= 1.
421
SET AUTOCOMMIT= 1;
422
# Take an exclusive lock.
423
# This would fail after timeout if t1 is still locked.
424
LOCK TABLE t1 IN SHARE MODE;
425
SET AUTOCOMMIT= 0;
426
COMMIT;
427
# connection default.
428
UNLOCK TABLES;
429
SET AUTOCOMMIT= 0;
430
COMMIT;
431
#
432
# With Auto commit on, transactional locks can time out.
433
# Default connection runs in transactional mode.
434
# Set AUTOCOMMIT= 0.
435
SET AUTOCOMMIT= 0;
436
# Take an exclusive lock, which persists.
437
LOCK TABLE t1 IN EXCLUSIVE MODE;
438
# connection conn1.
439
# conn1 runs in autocommit mode.
440
# Set AUTOCOMMIT= 1.
441
SET AUTOCOMMIT= 1;
442
# Try an exclusive lock,
443
# which times out though running in autocommit mode.
444
LOCK TABLE t1 IN SHARE MODE;
445
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
446
SET AUTOCOMMIT= 0;
447
COMMIT;
448
# connection default.
449
UNLOCK TABLES;
450
SET AUTOCOMMIT= 0;
451
COMMIT;
452
#
453
# Normal WRITE locks go before readers (autocommit).
454
# Set AUTOCOMMIT= 1.
455
SET AUTOCOMMIT= 1;
456
# Insert a value.
457
INSERT INTO t1 VALUES(111);
458
# Take a non-transactional lock.
459
LOCK TABLE t1 READ;
460
# connection conn1.
461
# Set AUTOCOMMIT= 1.
462
SET AUTOCOMMIT= 1;
463
# Take a non-transactional WRITE lock,
464
# which waits in background until first read lock is released.
465
LOCK TABLE t1 WRITE;
466
# connection default.
467
# Wait for the helper thread to sit on its lock.
468
# connection conn2.
469
# Set AUTOCOMMIT= 1.
470
SET AUTOCOMMIT= 1;
471
# Take a non-transactional READ lock,
472
# which waits in background until the WRITE lock is released.
473
LOCK TABLE t1 READ;
474
# connection default.
475
# Wait for the helper threads to sit on their locks.
476
# Unlock this connections non-transactional lock.
477
UNLOCK TABLES;
478
# connection conn1.
479
# Now the WRITE lock is taken.
480
# Insert a value.
481
INSERT INTO t1 VALUES(1111);
482
# Unlock table.
483
UNLOCK TABLES;
484
# connection conn2.
485
# Now the READ lock is taken.
486
# Select from the table.
487
SELECT * FROM t1;
488
c1
489
111
490
1111
491
# Unlock table.
492
UNLOCK TABLES;
493
# connection default.
494
TRUNCATE t1;
495
#
496
# LOW_PRIORITY WRITE locks wait for readers (autocommit).
497
# Insert a value.
498
INSERT INTO t1 VALUES(111);
499
# Take a non-transactional lock.
500
LOCK TABLE t1 READ;
501
# connection conn1.
502
# Take a non-transactional LOW_PRIORITY WRITE lock,
503
# which waits in background until all read locks are released.
504
LOCK TABLE t1 LOW_PRIORITY WRITE;
505
# connection default.
506
# Wait for the helper thread to sit on its lock.
507
# connection conn2.
508
# Take a non-transactional READ lock,
509
# which goes before the LOW_PRIORITY WRITE lock.
510
LOCK TABLE t1 READ;
511
# The READ lock could be taken immediately.
512
# Select from the table.
513
SELECT * FROM t1;
514
c1
515
111
516
# Unlock table.
517
UNLOCK TABLES;
518
SET AUTOCOMMIT= 0;
519
# connection default.
520
# Unlock this connections non-transactional lock.
521
UNLOCK TABLES;
522
# connection conn1.
523
# Now the LOW_PRIORITY WRITE lock is taken.
524
# Insert a value.
525
INSERT INTO t1 VALUES(1111);
526
# Unlock table.
527
UNLOCK TABLES;
528
SET AUTOCOMMIT= 0;
529
# connection default.
530
TRUNCATE t1;
531
SET AUTOCOMMIT= 0;
532
COMMIT;
533
#
534
# Normal WRITE locks go before readers (transaction).
535
# Insert a value.
536
INSERT INTO t1 VALUES(111);
537
COMMIT;
538
# Take a non-transactional lock.
539
LOCK TABLE t1 READ;
540
# connection conn1.
541
# Take a non-transactional WRITE lock,
542
# which waits in background until first read lock is released.
543
LOCK TABLE t1 WRITE;
544
# connection default.
545
# Wait for the helper thread to sit on its lock.
546
# connection conn2.
547
# Take a non-transactional READ lock,
548
# which waits in background until the WRITE lock is released.
549
LOCK TABLE t1 READ;
550
# connection default.
551
# Wait for the helper threads to sit on their locks.
552
# Unlock this connections non-transactional lock.
553
UNLOCK TABLES;
554
# connection conn1.
555
# Now the WRITE lock is taken.
556
# Insert a value.
557
INSERT INTO t1 VALUES(1111);
558
# Unlock table.
559
UNLOCK TABLES;
560
# connection conn2.
561
# Now the READ lock is taken.
562
# Select from the table.
563
SELECT * FROM t1;
564
c1
565
111
566
1111
567
# Unlock table.
568
UNLOCK TABLES;
569
# connection default.
570
TRUNCATE t1;
571
COMMIT;
572
#
573
# LOW_PRIORITY WRITE behaves like WRITE in transaction mode.
574
# Insert a value.
575
INSERT INTO t1 VALUES(111);
576
COMMIT;
577
# Take a non-transactional lock.
578
LOCK TABLE t1 READ;
579
# connection conn1.
580
# Take a non-transactional LOW_PRIORITY WRITE lock,
581
# which waits in background until first read lock is released.
582
LOCK TABLE t1 LOW_PRIORITY WRITE;
583
# connection default.
584
# Wait for the helper thread to sit on its lock.
585
# connection conn2.
586
# Take a non-transactional READ lock,
587
# which waits in background for the LOW_PRIORITY WRITE lock.
588
LOCK TABLE t1 READ;
589
# connection default.
590
# Wait for the helper threads to sit on their locks.
591
# Unlock this connections non-transactional lock.
592
UNLOCK TABLES;
593
# connection conn1.
594
# Now the LOW_PRIORITY WRITE lock is taken.
595
# Insert a value.
596
INSERT INTO t1 VALUES(1111);
597
# Unlock table.
598
UNLOCK TABLES;
599
# connection conn2.
600
# Now the READ lock is taken.
601
# Select from the table.
602
SELECT * FROM t1;
603
c1
604
111
605
1111
606
# Unlock table.
607
UNLOCK TABLES;
608
# connection default.
609
TRUNCATE t1;
610
COMMIT;
611
#
612
# NOWAIT.
613
# Take an exclusive lock.
614
LOCK TABLE t1 IN EXCLUSIVE MODE;
615
# connection conn1.
616
# Try an exclusive lock,
617
# which conflicts and cannot immediately be taken.
618
LOCK TABLE t1 IN SHARE MODE NOWAIT;
619
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
620
# connection default.
621
# Commit.
622
COMMIT;
623
#
624
# Transactional table locks do not interfere with the global read lock.
625
# Take an exclusive lock on t1.
626
LOCK TABLE t1 IN EXCLUSIVE MODE;
627
# connection conn1.
628
# Try an exclusive lock, which conflicts.
629
LOCK TABLE t1 IN EXCLUSIVE MODE;
630
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
631
# Can take the global read lock when an exclusive lock exist.
632
FLUSH TABLES WITH READ LOCK;
633
# Show that the global read lock exists.
634
LOCK TABLE t2 WRITE;
635
ERROR HY000: Can't execute the query because you have a conflicting read lock
636
# connection default.
637
# Can take an exclusive lock when the global read lock exists.
638
# Take an exclusive lock on t2.
639
LOCK TABLE t2 IN EXCLUSIVE MODE;
640
# connection conn1.
641
# Show that an exclusive lock on t1 exists.
642
LOCK TABLE t1 IN EXCLUSIVE MODE;
643
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
644
# Show that an exclusive lock on t2 exists.
645
LOCK TABLE t2 IN EXCLUSIVE MODE;
646
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
647
# Show that the global read lock exists.
648
LOCK TABLE t2 WRITE;
649
ERROR HY000: Can't execute the query because you have a conflicting read lock
650
# Release global read lock.
651
UNLOCK TABLES;
652
# Commit.
653
COMMIT;
654
# connection default.
655
UNLOCK TABLES;
656
# Commit.
657
COMMIT;
658
#
659
# Access conflict on INSERT.
660
# Take an share lock on t1.
661
LOCK TABLE t1 IN SHARE MODE;
662
# connection conn1.
663
SELECT GET_LOCK("mysqltest1", 10);
664
GET_LOCK("mysqltest1", 10)
665
1
666
# Try to insert a value,
667
# which must wait in background for the lock to go away.
668
INSERT INTO t1 VALUES (111);
669
## connection default.
670
## Wait in background until the insert times out and releases lock.
671
SELECT GET_LOCK("mysqltest1", 10);
672
# connection conn1.
673
# Wait for INSERT to timeout.
674
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
675
SELECT RELEASE_LOCK("mysqltest1");
676
RELEASE_LOCK("mysqltest1")
677
1
678
COMMIT;
679
# connection default.
680
GET_LOCK("mysqltest1", 10)
681
1
682
# Commit.
683
COMMIT;
684
# Show that the insert in conn1 failed.
685
SELECT * FROM t1;
686
c1
687
#
688
# Access conflict on UPDATE with exclusive lock.
689
# Insert a value.
690
INSERT INTO t1 VALUES (111);
691
COMMIT;
692
# connection conn1.
693
# Take an exclusive lock.
694
LOCK TABLE t1 IN EXCLUSIVE MODE;
695
# connection default.
696
# Try a second exclusive lock, which fails due to the other lock.
697
LOCK TABLE t1 IN EXCLUSIVE MODE;
698
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
699
# Try an update, which fails due to the exclusive lock.
700
UPDATE t1 SET c1= 111222;
701
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
702
# connection conn1.
703
# The exclusive table locker can still update.
704
UPDATE t1 SET c1= 111333;
705
# connection default.
706
# Select is allowed despite the table lock, but sees old data.
707
SELECT * FROM t1;
708
c1
709
111
710
# connection conn1.
711
# Commit.
712
COMMIT;
713
# connection default.
714
# It seems like the failed update began a transaction, so still old data.
715
SELECT * FROM t1;
716
c1
717
111
718
# Commit.
719
COMMIT;
720
# Now select sees current data.
721
SELECT * FROM t1;
722
c1
723
111333
724
TRUNCATE t1;
725
COMMIT;
726
#
727
# Access conflict on UPDATE with share lock.
728
# Insert a value.
729
INSERT INTO t1 VALUES (111);
730
COMMIT;
731
# connection conn1.
732
# Take a share lock.
733
LOCK TABLE t1 IN SHARE MODE;
734
# Update with a single share lock is possible.
735
UPDATE t1 SET c1= 111222;
736
# Commit to get rid of the row lock.
737
COMMIT;
738
# Take a share lock.
739
LOCK TABLE t1 IN SHARE MODE;
740
# connection default.
741
# An exclusive lock is not possible on a share lock.
742
LOCK TABLE t1 IN EXCLUSIVE MODE;
743
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
744
# More share locks are possible.
745
LOCK TABLE t1 IN SHARE MODE;
746
# Noone can update when multiple share locks exist.
747
UPDATE t1 SET c1= 111333;
748
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
749
# connection conn1.
750
# Noone can update when multiple share locks exist.
751
UPDATE t1 SET c1= 111444;
752
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
753
COMMIT;
754
# connection default.
755
SELECT * FROM t1;
756
c1
757
111222
758
TRUNCATE t1;
759
COMMIT;
760
#
761
# LOCK TABLE is prohibited in stored procedure.
762
CREATE PROCEDURE lock_t1_excl()
763
LOCK TABLE t1 IN EXCLUSIVE MODE;
764
ERROR 0A000: LOCK is not allowed in stored procedures
765
#
766
# LOCK TABLE is prohibited in trigger.
767
CREATE TRIGGER t1_ai AFTER INSERT ON t1 FOR EACH ROW
768
LOCK TABLE t2 IN EXCLUSIVE MODE;
769
ERROR 0A000: LOCK is not allowed in stored procedures
770
#
771
# LOCK TABLE on a pre-locked table through a trigger.
772
# Create a trigger on t1 that updates t2.
773
CREATE TRIGGER t1_ai AFTER INSERT ON t1 FOR EACH ROW
774
BEGIN
775
UPDATE t2 SET c2= c2 + 111;
776
END//
777
# Take an exclusive lock on t1.
778
# This pre-locks t2 through the trigger.
779
LOCK TABLE t1 IN EXCLUSIVE MODE;
780
# connection conn1.
781
# Try to take an exclusive lock on t2,
782
# which is pre-locked through the trigger on t1.
783
LOCK TABLE t2 IN EXCLUSIVE MODE;
784
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
785
COMMIT;
786
# connection default.
787
# Commit to release the lock.
788
COMMIT;
789
DROP TRIGGER t1_ai;
790
#
791
# LOCK TABLE on a pre-locked table through a view.
792
# Create a function that selects from t2.
793
CREATE FUNCTION count_t2() RETURNS INT
794
BEGIN
795
RETURN (SELECT COUNT(*) FROM t2);
796
END//
797
# Create a view with t1 and the function.
798
CREATE VIEW v1 AS SELECT COUNT(*), count_t2() FROM t1 GROUP BY 2;
799
# Take an exclusive lock on v1.
800
# This pre-locks t2 through the view.
801
LOCK TABLE v1 IN EXCLUSIVE MODE;
802
# connection conn1.
803
# Try to take an exclusive lock on t2,
804
# which is pre-locked through the function in v1.
805
LOCK TABLE t2 IN EXCLUSIVE MODE;
806
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
807
COMMIT;
808
# connection default.
809
# Commit to release the lock.
810
COMMIT;
811
DROP VIEW v1;
812
DROP FUNCTION count_t2;
813
#
814
# Transactional LOCK TABLE by-passes pre-lock.
815
#
816
# Insert a value to t1 and t2 each.
817
INSERT INTO t1 VALUES (111);
818
INSERT INTO t2 VALUES (222);
819
COMMIT;
820
# Create a trigger on t1 that updates t2.
821
CREATE TRIGGER t1_ai AFTER INSERT ON t1 FOR EACH ROW
822
BEGIN
823
INSERT INTO t2 SELECT GET_LOCK("mysqltest1", 10);
824
UPDATE t2 SET c2= c2 + 111;
825
INSERT INTO t2 SELECT RELEASE_LOCK("mysqltest1");
826
END//
827
# Take an SQL lock which blocks the trigger.
828
SELECT GET_LOCK("mysqltest1", 10);
829
GET_LOCK("mysqltest1", 10)
830
1
831
# connection conn1.
832
# Insert into t1 to fire trigger. This waits on GET_LOCK.
833
INSERT INTO t1 VALUES(111222);
834
# connection default.
835
# Wait for the helper thread to sit on its lock.
836
# Take an exclusive lock.
837
LOCK TABLE t2 IN EXCLUSIVE MODE;
838
# Use the lock for insert.
839
INSERT INTO t2 VALUES (111333);
840
# Commit to release the lock again.
841
COMMIT;
842
# Release the SQL lock to let the trigger finish.
843
SELECT RELEASE_LOCK("mysqltest1");
844
RELEASE_LOCK("mysqltest1")
845
1
846
# connection conn1.
847
# Trigger succeeded.
848
# Commit.
849
COMMIT;
850
# connection default.
851
# Commit.
852
COMMIT;
853
# Show the results.
854
SELECT * FROM t1;
855
c1
856
111
857
111222
858
SELECT * FROM t2;
859
c2
860
333
861
111444
862
112
863
1
864
TRUNCATE t1;
865
TRUNCATE t2;
866
COMMIT;
867
DROP TRIGGER t1_ai;
868
#
869
# Non-transactional LOCK TABLE cannot by-passes pre-lock.
870
#
871
# Insert a value to t1 and t2 each.
872
INSERT INTO t1 VALUES (111);
873
INSERT INTO t2 VALUES (222);
874
COMMIT;
875
# Create a trigger on t1 that updates t2.
876
CREATE TRIGGER t1_ai AFTER INSERT ON t1 FOR EACH ROW
877
BEGIN
878
# This will time out. So use a small value.
879
INSERT INTO t2 SELECT GET_LOCK("mysqltest1", 1);
880
UPDATE t2 SET c2= c2 + 111;
881
INSERT INTO t2 SELECT RELEASE_LOCK("mysqltest1");
882
END//
883
# Take an SQL lock which blocks the trigger.
884
SELECT GET_LOCK("mysqltest1", 10);
885
GET_LOCK("mysqltest1", 10)
886
1
887
# connection conn1.
888
# Insert into t1 to fire trigger. This waits on GET_LOCK.
889
INSERT INTO t1 VALUES(111222);
890
# connection default.
891
# Wait for the helper thread to sit on its lock.
892
# Take a write lock. This waits until the trigger times out.
893
LOCK TABLE t2 WRITE;
894
# Use the lock for insert.
895
INSERT INTO t2 VALUES (111333);
896
# Release the lock again.
897
UNLOCK TABLES;
898
# Release the SQL lock.
899
SELECT RELEASE_LOCK("mysqltest1");
900
RELEASE_LOCK("mysqltest1")
901
1
902
# connection conn1.
903
# Trigger timed out.
904
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
905
# Commit.
906
COMMIT;
907
# connection default.
908
# Commit.
909
COMMIT;
910
# Show the results.
911
SELECT * FROM t1;
912
c1
913
111
914
SELECT * FROM t2;
915
c2
916
222
917
111333
918
TRUNCATE t1;
919
TRUNCATE t2;
920
COMMIT;
921
DROP TRIGGER t1_ai;
922
## Cleanup.
923
SET AUTOCOMMIT= 1;
924
UNLOCK TABLES;
925
DROP TABLE t1, t2;