MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
deadlock.inc
1 # include/deadlock.inc
2 #
3 # The variable
4 # $engine_type -- storage engine to be tested
5 # has to be set before sourcing this script.
6 #
7 # Last update:
8 # 2006-07-26 ML refactoring + print when connection is switched
9 # old name was t/innodb-deadlock.test
10 # main code went into include/deadlock.inc
11 #
12 
13 --echo # Establish connection con1 (user=root)
14 connect (con1,localhost,root,,);
15 --echo # Establish connection con2 (user=root)
16 connect (con2,localhost,root,,);
17 
18 --disable_warnings
19 drop table if exists t1,t2;
20 --enable_warnings
21 
22 #
23 # Testing of FOR UPDATE
24 #
25 
26 --echo # Switch to connection con1
27 connection con1;
28 eval create table t1 (id integer, x integer) engine = $engine_type;
29 insert into t1 values(0, 0);
30 set autocommit=0;
31 SELECT * from t1 where id = 0 FOR UPDATE;
32 
33 --echo # Switch to connection con2
34 connection con2;
35 set autocommit=0;
36 
37 # The following query should hang because con1 is locking the record
38 --send
39 update t1 set x=2 where id = 0;
40 --sleep 2
41 
42 --echo # Switch to connection con1
43 connection con1;
44 update t1 set x=1 where id = 0;
45 select * from t1;
46 commit;
47 
48 --echo # Switch to connection con2
49 connection con2;
50 reap;
51 commit;
52 
53 --echo # Switch to connection con1
54 connection con1;
55 select * from t1;
56 commit;
57 
58 drop table t1;
59 #
60 # Testing of FOR UPDATE
61 #
62 
63 --echo # Switch to connection con1
64 connection con1;
65 eval create table t1 (id integer, x integer) engine = $engine_type;
66 eval create table t2 (b integer, a integer) engine = $engine_type;
67 insert into t1 values(0, 0), (300, 300);
68 insert into t2 values(0, 10), (1, 20), (2, 30);
69 commit;
70 set autocommit=0;
71 select * from t2;
72 update t2 set a=100 where b=(SELECT x from t1 where id = b FOR UPDATE);
73 select * from t2;
74 select * from t1;
75 
76 --echo # Switch to connection con2
77 connection con2;
78 set autocommit=0;
79 
80 # The following query should hang because con1 is locking the record
81 --send
82 update t1 set x=2 where id = 0;
83 --sleep 2
84 
85 --echo # Switch to connection con1
86 connection con1;
87 update t1 set x=1 where id = 0;
88 select * from t1;
89 commit;
90 
91 --echo # Switch to connection con2
92 connection con2;
93 reap;
94 commit;
95 
96 --echo # Switch to connection con1
97 connection con1;
98 select * from t1;
99 commit;
100 
101 drop table t1, t2;
102 eval create table t1 (id integer, x integer) engine = $engine_type;
103 eval create table t2 (b integer, a integer) engine = $engine_type;
104 insert into t1 values(0, 0), (300, 300);
105 insert into t2 values(0, 0), (1, 20), (2, 30);
106 commit;
107 
108 --echo # Switch to connection con1
109 connection con1;
110 select a,b from t2 UNION SELECT id, x from t1 FOR UPDATE;
111 select * from t2;
112 select * from t1;
113 
114 --echo # Switch to connection con2
115 connection con2;
116 
117 # The following query should hang because con1 is locking the record
118 update t2 set a=2 where b = 0;
119 select * from t2;
120 --send
121 update t1 set x=2 where id = 0;
122 --sleep 2
123 
124 --echo # Switch to connection con1
125 connection con1;
126 update t1 set x=1 where id = 0;
127 select * from t1;
128 commit;
129 
130 --echo # Switch to connection con2
131 connection con2;
132 reap;
133 commit;
134 
135 --echo # Switch to connection con1
136 connection con1;
137 select * from t1;
138 commit;
139 
140 # Cleanup
141 --echo # Switch to connection default + disconnect con1 and con2
142 connection default;
143 disconnect con1;
144 disconnect con2;
145 drop table t1, t2;
146 
147 --echo End of 4.1 tests
148 
149 #
150 # Bug#25164 create table `a` as select * from `A` hangs
151 #
152 
153 set default_storage_engine=innodb;
154 
155 --disable_warnings
156 drop table if exists a;
157 drop table if exists A;
158 --enable_warnings
159 
160 create table A (c int);
161 insert into A (c) values (0);
162 --error 0,ER_LOCK_DEADLOCK,ER_TABLE_EXISTS_ERROR
163 create table a as select * from A;
164 drop table A;
165 
166 --disable_warnings
167 drop table if exists a;
168 --enable_warnings
169 
170 set default_storage_engine=default;
171 
172 --echo End of 5.0 tests.