001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.db;
007:
008: import java.sql.Connection;
009: import java.sql.DriverManager;
010: import java.sql.PreparedStatement;
011: import java.sql.ResultSet;
012: import java.sql.SQLException;
013: import java.sql.Statement;
014:
015: import org.h2.test.TestBase;
016: import org.h2.tools.CreateCluster;
017: import org.h2.tools.DeleteDbFiles;
018: import org.h2.tools.Server;
019:
020: /**
021: * Test for the cluster feature.
022: */
023: public class TestCluster extends TestBase {
024:
025: public void test() throws Exception {
026: if (config.memory || config.networked) {
027: return;
028: }
029:
030: DeleteDbFiles.main(new String[] { "-dir", baseDir + "/node1",
031: "-quiet" });
032: DeleteDbFiles.main(new String[] { "-dir", baseDir + "/node2",
033: "-quiet" });
034:
035: // create the master database
036: Connection conn;
037: Class.forName("org.h2.Driver");
038: conn = DriverManager.getConnection("jdbc:h2:file:" + baseDir
039: + "/node1/test", "sa", "");
040: Statement stat;
041: stat = conn.createStatement();
042: stat.execute("DROP TABLE IF EXISTS TEST");
043: stat
044: .execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
045: PreparedStatement prep = conn
046: .prepareStatement("INSERT INTO TEST VALUES(?, ?)");
047: int len = getSize(10, 1000);
048: for (int i = 0; i < len; i++) {
049: prep.setInt(1, i);
050: prep.setString(2, "Data" + i);
051: prep.executeUpdate();
052: }
053: check(conn, len);
054: conn.close();
055:
056: CreateCluster.main(new String[] { "-urlSource",
057: "jdbc:h2:file:" + baseDir + "/node1/test",
058: "-urlTarget",
059: "jdbc:h2:file:" + baseDir + "/node2/test", "-user",
060: "sa", "-serverlist", "localhost:9191,localhost:9192" });
061: Server n1 = org.h2.tools.Server.createTcpServer(
062: new String[] { "-tcpPort", "9191", "-baseDir",
063: baseDir + "/node1" }).start();
064: Server n2 = org.h2.tools.Server.createTcpServer(
065: new String[] { "-tcpPort", "9192", "-baseDir",
066: baseDir + "/node2" }).start();
067:
068: try {
069: conn = DriverManager.getConnection(
070: "jdbc:h2:tcp://localhost:9191/test", "sa", "");
071: error("should not be able to connect in standalone mode");
072: } catch (SQLException e) {
073: checkNotGeneralException(e);
074: }
075:
076: try {
077: conn = DriverManager.getConnection(
078: "jdbc:h2:tcp://localhost:9192/test", "sa", "");
079: error("should not be able to connect in standalone mode");
080: } catch (SQLException e) {
081: checkNotGeneralException(e);
082: }
083:
084: // test regular cluster connection
085: conn = DriverManager.getConnection(
086: "jdbc:h2:tcp://localhost:9191,localhost:9192/test",
087: "sa", "");
088: check(conn, len);
089: conn.close();
090:
091: // test if only one server is available at the beginning
092: n2.stop();
093: conn = DriverManager.getConnection(
094: "jdbc:h2:tcp://localhost:9191,localhost:9192/test",
095: "sa", "");
096: stat = conn.createStatement();
097: check(conn, len);
098: conn.close();
099:
100: // disable the cluster
101: conn = DriverManager.getConnection(
102: "jdbc:h2:tcp://localhost:9191/test;CLUSTER=''", "sa",
103: "");
104: conn.close();
105: n1.stop();
106:
107: // re-create the cluster
108: DeleteDbFiles.main(new String[] { "-dir", baseDir + "/node2",
109: "-quiet" });
110: CreateCluster.main(new String[] { "-urlSource",
111: "jdbc:h2:file:" + baseDir + "/node1/test",
112: "-urlTarget",
113: "jdbc:h2:file:" + baseDir + "/node2/test", "-user",
114: "sa", "-serverlist", "localhost:9191,localhost:9192" });
115: n1 = org.h2.tools.Server.createTcpServer(
116: new String[] { "-tcpPort", "9191", "-baseDir",
117: baseDir + "/node1" }).start();
118: n2 = org.h2.tools.Server.createTcpServer(
119: new String[] { "-tcpPort", "9192", "-baseDir",
120: baseDir + "/node2" }).start();
121:
122: conn = DriverManager.getConnection(
123: "jdbc:h2:tcp://localhost:9191,localhost:9192/test",
124: "sa", "");
125: stat = conn.createStatement();
126: stat.execute("CREATE TABLE BOTH(ID INT)");
127:
128: n1.stop();
129:
130: stat.execute("CREATE TABLE A(ID INT)");
131: conn.close();
132: n2.stop();
133:
134: n1 = org.h2.tools.Server.createTcpServer(
135: new String[] { "-tcpPort", "9191", "-baseDir",
136: baseDir + "/node1" }).start();
137: conn = DriverManager.getConnection(
138: "jdbc:h2:tcp://localhost:9191/test;CLUSTER=''", "sa",
139: "");
140: check(conn, len);
141: conn.close();
142: n1.stop();
143:
144: n2 = org.h2.tools.Server.createTcpServer(
145: new String[] { "-tcpPort", "9192", "-baseDir",
146: baseDir + "/node2" }).start();
147: conn = DriverManager.getConnection(
148: "jdbc:h2:tcp://localhost:9192/test;CLUSTER=''", "sa",
149: "");
150: check(conn, len);
151: conn.createStatement().execute("SELECT * FROM A");
152: conn.close();
153: n2.stop();
154: }
155:
156: void check(Connection conn, int len) throws Exception {
157: PreparedStatement prep = conn
158: .prepareStatement("SELECT * FROM TEST WHERE ID=?");
159: for (int i = 0; i < len; i++) {
160: prep.setInt(1, i);
161: ResultSet rs = prep.executeQuery();
162: rs.next();
163: check(rs.getString(2), "Data" + i);
164: checkFalse(rs.next());
165: }
166: }
167:
168: }
|