XATest.java
15.6 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
The MySQL Connector/J is licensed under the terms of the GPLv2
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
this software, see the FOSS License Exception
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; version 2
of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this
program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
Floor, Boston, MA 02110-1301 USA
*/
package testsuite.simple;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.rmi.server.UID;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
import javax.sql.XAConnection;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlXid;
import testsuite.BaseTestCase;
/**
* Unit tests for our XA implementation.
*/
public class XATest extends BaseTestCase {
MysqlXADataSource xaDs;
public XATest(String name) {
super(name);
this.xaDs = new MysqlXADataSource();
this.xaDs.setUrl(BaseTestCase.dbUrl);
this.xaDs.setRollbackOnPooledClose(true);
}
/**
* Tests that simple distributed transaction processing works as expected.
*
* @throws Exception
* if the test fails.
*/
public void testCoordination() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
}
createTable("testCoordination", "(field1 int) ENGINE=InnoDB");
Connection conn1 = null;
Connection conn2 = null;
XAConnection xaConn1 = null;
XAConnection xaConn2 = null;
try {
xaConn1 = getXAConnection();
XAResource xaRes1 = xaConn1.getXAResource();
conn1 = xaConn1.getConnection();
xaConn2 = getXAConnection();
XAResource xaRes2 = xaConn2.getXAResource();
conn2 = xaConn2.getConnection();
Xid xid1 = createXid();
Xid xid2 = createXid(xid1);
xaRes1.start(xid1, XAResource.TMNOFLAGS);
xaRes2.start(xid2, XAResource.TMNOFLAGS);
conn1.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (1)");
conn2.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (2)");
xaRes1.end(xid1, XAResource.TMSUCCESS);
xaRes2.end(xid2, XAResource.TMSUCCESS);
xaRes1.prepare(xid1);
xaRes2.prepare(xid2);
xaRes1.commit(xid1, false);
xaRes2.commit(xid2, false);
this.rs = this.stmt.executeQuery("SELECT field1 FROM testCoordination ORDER BY field1");
assertTrue(this.rs.next());
assertEquals(1, this.rs.getInt(1));
assertTrue(this.rs.next());
assertEquals(2, this.rs.getInt(1));
this.stmt.executeUpdate("TRUNCATE TABLE testCoordination");
//
// Now test rollback
//
xid1 = createXid();
xid2 = createXid(xid1);
xaRes1.start(xid1, XAResource.TMNOFLAGS);
xaRes2.start(xid2, XAResource.TMNOFLAGS);
conn1.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (1)");
// ensure visibility
assertEquals("1", getSingleIndexedValueWithQuery(conn1, 1, "SELECT field1 FROM testCoordination WHERE field1=1").toString());
conn2.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (2)");
// ensure visibility
assertEquals("2", getSingleIndexedValueWithQuery(conn2, 1, "SELECT field1 FROM testCoordination WHERE field1=2").toString());
xaRes1.end(xid1, XAResource.TMSUCCESS);
xaRes2.end(xid2, XAResource.TMSUCCESS);
xaRes1.prepare(xid1);
xaRes2.prepare(xid2);
xaRes1.rollback(xid1);
xaRes2.rollback(xid2);
this.rs = this.stmt.executeQuery("SELECT field1 FROM testCoordination ORDER BY field1");
assertTrue(!this.rs.next());
} finally {
if (conn1 != null) {
conn1.close();
}
if (conn2 != null) {
conn2.close();
}
if (xaConn1 != null) {
xaConn1.close();
}
if (xaConn2 != null) {
xaConn2.close();
}
}
}
protected XAConnection getXAConnection() throws Exception {
return this.xaDs.getXAConnection();
}
/**
* Tests that XA RECOVER works as expected.
*
* @throws Exception
* if test fails
*/
public void testRecover() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
}
if (versionMeetsMinimum(5, 7) && !versionMeetsMinimum(5, 7, 5)) {
// Test is broken in 5.7.0 - 5.7.4 after server bug#14670465 fix which changed the XA RECOVER output format.
// Fixed in 5.7.5 server version
return;
}
// BUG#14670465 fix broke this functionality in 5.7.1 - 5.7.2
if (versionMeetsMinimum(5, 7, 1) && !versionMeetsMinimum(5, 7, 3)) {
return;
}
XAConnection xaConn = null, recoverConn = null;
try {
xaConn = getXAConnection();
Connection c = xaConn.getConnection();
Xid xid = createXid();
XAResource xaRes = xaConn.getXAResource();
xaRes.start(xid, XAResource.TMNOFLAGS);
c.createStatement().execute("SELECT 1");
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.prepare(xid);
// Now try and recover
recoverConn = getXAConnection();
XAResource recoverRes = recoverConn.getXAResource();
Xid[] recoveredXids = recoverRes.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN);
assertTrue(recoveredXids != null);
assertTrue(recoveredXids.length > 0);
boolean xidFound = false;
for (int i = 0; i < recoveredXids.length; i++) {
if (recoveredXids[i] != null && recoveredXids[i].equals(xid)) {
xidFound = true;
break;
}
}
assertTrue(xidFound);
recoverRes = recoverConn.getXAResource();
recoveredXids = recoverRes.recover(XAResource.TMSTARTRSCAN);
assertTrue(recoveredXids != null);
assertTrue(recoveredXids.length > 0);
xidFound = false;
for (int i = 0; i < recoveredXids.length; i++) {
if (recoveredXids[i] != null && recoveredXids[i].equals(xid)) {
xidFound = true;
break;
}
}
assertTrue(xidFound);
// Test flags
recoverRes.recover(XAResource.TMSTARTRSCAN);
recoverRes.recover(XAResource.TMENDRSCAN);
recoverRes.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN);
// This should fail
try {
recoverRes.recover(XAResource.TMSUCCESS);
fail("XAException should have been thrown");
} catch (XAException xaEx) {
assertEquals(XAException.XAER_INVAL, xaEx.errorCode);
}
} finally {
if (xaConn != null) {
xaConn.close();
}
if (recoverConn != null) {
recoverConn.close();
}
}
}
/**
* Tests operation of local transactions on XAConnections when global
* transactions are in or not in progress (follows from BUG#17401).
*
* @throws Exception
* if the testcase fails
*/
public void testLocalTransaction() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
}
createTable("testLocalTransaction", "(field1 int) ENGINE=InnoDB");
Connection conn1 = null;
XAConnection xaConn1 = null;
try {
xaConn1 = getXAConnection();
XAResource xaRes1 = xaConn1.getXAResource();
conn1 = xaConn1.getConnection();
assertEquals(true, conn1.getAutoCommit());
conn1.setAutoCommit(true);
conn1.createStatement().executeUpdate("INSERT INTO testLocalTransaction VALUES (1)");
assertEquals("1", getSingleIndexedValueWithQuery(conn1, 1, "SELECT field1 FROM testLocalTransaction").toString());
conn1.createStatement().executeUpdate("TRUNCATE TABLE testLocalTransaction");
conn1.setAutoCommit(false);
conn1.createStatement().executeUpdate("INSERT INTO testLocalTransaction VALUES (2)");
assertEquals("2", getSingleIndexedValueWithQuery(conn1, 1, "SELECT field1 FROM testLocalTransaction").toString());
conn1.rollback();
assertEquals(0, getRowCount("testLocalTransaction"));
conn1.createStatement().executeUpdate("INSERT INTO testLocalTransaction VALUES (3)");
assertEquals("3", getSingleIndexedValueWithQuery(conn1, 1, "SELECT field1 FROM testLocalTransaction").toString());
conn1.commit();
assertEquals("3", getSingleIndexedValueWithQuery(conn1, 1, "SELECT field1 FROM testLocalTransaction").toString());
conn1.commit();
Savepoint sp = conn1.setSavepoint();
conn1.rollback(sp);
sp = conn1.setSavepoint("abcd");
conn1.rollback(sp);
Savepoint spSaved = sp;
Xid xid = createXid();
xaRes1.start(xid, XAResource.TMNOFLAGS);
try {
try {
conn1.setAutoCommit(true);
} catch (SQLException sqlEx) {
// we expect an exception here
assertEquals("2D000", sqlEx.getSQLState());
}
try {
conn1.commit();
} catch (SQLException sqlEx) {
// we expect an exception here
assertEquals("2D000", sqlEx.getSQLState());
}
try {
conn1.rollback();
} catch (SQLException sqlEx) {
// we expect an exception here
assertEquals("2D000", sqlEx.getSQLState());
}
try {
sp = conn1.setSavepoint();
} catch (SQLException sqlEx) {
// we expect an exception here
assertEquals("2D000", sqlEx.getSQLState());
}
try {
conn1.rollback(spSaved);
} catch (SQLException sqlEx) {
// we expect an exception here
assertEquals("2D000", sqlEx.getSQLState());
}
try {
sp = conn1.setSavepoint("abcd");
} catch (SQLException sqlEx) {
// we expect an exception here
assertEquals("2D000", sqlEx.getSQLState());
}
try {
conn1.rollback(spSaved);
} catch (SQLException sqlEx) {
// we expect an exception here
assertEquals("2D000", sqlEx.getSQLState());
}
} finally {
xaRes1.forget(xid);
}
} finally {
if (xaConn1 != null) {
try {
xaConn1.close();
} catch (SQLException sqlEx) {
// this is just busted in the server right now
}
}
}
}
public void testSuspendableTx() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
}
Connection conn1 = null;
MysqlXADataSource suspXaDs = new MysqlXADataSource();
suspXaDs.setUrl(BaseTestCase.dbUrl);
suspXaDs.setPinGlobalTxToPhysicalConnection(true);
suspXaDs.setRollbackOnPooledClose(true);
XAConnection xaConn1 = null;
Xid xid = createXid();
try {
/*
* -- works using RESUME
* xa start 0x123,0x456;
* select * from foo;
* xa end 0x123,0x456;
* xa start 0x123,0x456 resume;
* select * from foo;
* xa end 0x123,0x456;
* xa commit 0x123,0x456 one phase;
*/
xaConn1 = suspXaDs.getXAConnection();
XAResource xaRes1 = xaConn1.getXAResource();
conn1 = xaConn1.getConnection();
xaRes1.start(xid, XAResource.TMNOFLAGS);
conn1.createStatement().execute("SELECT 1");
xaRes1.end(xid, XAResource.TMSUCCESS);
xaRes1.start(xid, XAResource.TMRESUME);
conn1.createStatement().execute("SELECT 1");
xaRes1.end(xid, XAResource.TMSUCCESS);
xaRes1.commit(xid, true);
xaConn1.close();
/*
*
* -- fails using JOIN
* xa start 0x123,0x456;
* select * from foo;
* xa end 0x123,0x456;
* xa start 0x123,0x456 join;
* select * from foo;
* xa end 0x123,0x456;
* xa commit 0x123,0x456 one phase;
*/
xaConn1 = suspXaDs.getXAConnection();
xaRes1 = xaConn1.getXAResource();
conn1 = xaConn1.getConnection();
xaRes1.start(xid, XAResource.TMNOFLAGS);
conn1.createStatement().execute("SELECT 1");
xaRes1.end(xid, XAResource.TMSUCCESS);
xaRes1.start(xid, XAResource.TMJOIN);
conn1.createStatement().execute("SELECT 1");
xaRes1.end(xid, XAResource.TMSUCCESS);
xaRes1.commit(xid, true);
} finally {
if (xaConn1 != null) {
xaConn1.close();
}
}
}
private Xid createXid() throws IOException {
ByteArrayOutputStream gtridOut = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(gtridOut);
new UID().write(dataOut);
final byte[] gtrid = gtridOut.toByteArray();
ByteArrayOutputStream bqualOut = new ByteArrayOutputStream();
dataOut = new DataOutputStream(bqualOut);
new UID().write(dataOut);
final byte[] bqual = bqualOut.toByteArray();
Xid xid = new MysqlXid(gtrid, bqual, 3306);
return xid;
}
private Xid createXid(Xid xidToBranch) throws IOException {
ByteArrayOutputStream bqualOut = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(bqualOut);
new UID().write(dataOut);
final byte[] bqual = bqualOut.toByteArray();
Xid xid = new MysqlXid(xidToBranch.getGlobalTransactionId(), bqual, 3306);
return xid;
}
}