01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.db;
07:
08: import java.sql.Connection;
09: import java.sql.ResultSet;
10: import java.sql.Statement;
11: import java.util.ArrayList;
12:
13: import org.h2.test.TestBase;
14:
15: /**
16: * Tests for the two-phase-commit feature.
17: */
18: public class TestTwoPhaseCommit extends TestBase {
19: public void test() throws Exception {
20: if (config.memory || config.networked || config.logMode == 0) {
21: return;
22: }
23:
24: deleteDb("twoPhaseCommit");
25:
26: prepare();
27: openWith(true);
28: test(true);
29:
30: prepare();
31: openWith(false);
32: test(false);
33: }
34:
35: void test(boolean rolledBack) throws Exception {
36: Connection conn = getConnection("twoPhaseCommit");
37: Statement stat = conn.createStatement();
38: stat.execute("SET WRITE_DELAY 0");
39: ResultSet rs = stat
40: .executeQuery("SELECT * FROM TEST ORDER BY ID");
41: rs.next();
42: check(rs.getInt(1), 1);
43: check(rs.getString(2), "Hello");
44: if (rolledBack) {
45: } else {
46: rs.next();
47: check(rs.getInt(1), 2);
48: check(rs.getString(2), "World");
49: }
50: checkFalse(rs.next());
51: conn.close();
52: }
53:
54: void openWith(boolean rollback) throws Exception {
55: Connection conn = getConnection("twoPhaseCommit");
56: Statement stat = conn.createStatement();
57: ArrayList list = new ArrayList();
58: ResultSet rs = stat
59: .executeQuery("SELECT * FROM INFORMATION_SCHEMA.IN_DOUBT");
60: while (rs.next()) {
61: list.add(rs.getString("TRANSACTION"));
62: }
63: for (int i = 0; i < list.size(); i++) {
64: String s = (String) list.get(i);
65: if (rollback) {
66: stat.execute("ROLLBACK TRANSACTION " + s);
67: } else {
68: stat.execute("COMMIT TRANSACTION " + s);
69: }
70: }
71: conn.close();
72: }
73:
74: void prepare() throws Exception {
75: deleteDb("twoPhaseCommit");
76: Connection conn = getConnection("twoPhaseCommit");
77: Statement stat = conn.createStatement();
78: stat.execute("SET WRITE_DELAY 0");
79: conn.setAutoCommit(false);
80: stat
81: .execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
82: stat.execute("INSERT INTO TEST VALUES(1, 'Hello')");
83: conn.commit();
84: stat.execute("INSERT INTO TEST VALUES(2, 'World')");
85: stat
86: .execute("PREPARE COMMIT XID_TEST_TRANSACTION_WITH_LONG_NAME");
87: crash(conn);
88: }
89: }
|