Beispiel #1
0
 private void deleteFiles() throws SQLException {
   DeleteDbFiles.main("-dir", getBaseDir() + "/node1", "-quiet");
   DeleteDbFiles.main("-dir", getBaseDir() + "/node2", "-quiet");
   IOUtils.delete(getBaseDir() + "/node1");
   IOUtils.delete(getBaseDir() + "/node2");
 }
Beispiel #2
0
  private void testStartStopCluster() throws SQLException {
    if (config.memory || config.networked || config.cipher != null) {
      return;
    }
    int port1 = 9193, port2 = 9194;
    String serverList = "localhost:" + port1 + ",localhost:" + port2;
    deleteFiles();

    // initialize the database
    Connection conn;
    org.h2.Driver.load();

    String urlNode1 = getURL("node1/test", true);
    String urlNode2 = getURL("node2/test", true);
    String user = getUser(), password = getPassword();
    conn = DriverManager.getConnection(urlNode1, user, password);
    Statement stat;
    stat = conn.createStatement();
    stat.execute("DROP TABLE IF EXISTS TEST");
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
    PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
    int len = getSize(10, 1000);
    for (int i = 0; i < len; i++) {
      prep.setInt(1, i);
      prep.setString(2, "Data" + i);
      prep.executeUpdate();
    }
    check(conn, len, "''");
    conn.close();

    // copy the database and initialize the cluster
    CreateCluster.main(
        "-urlSource",
        urlNode1,
        "-urlTarget",
        urlNode2,
        "-user",
        user,
        "-password",
        password,
        "-serverList",
        serverList);

    // start both servers
    Server n1 =
        org.h2.tools.Server.createTcpServer(
                "-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1")
            .start();
    Server n2 =
        org.h2.tools.Server.createTcpServer(
                "-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2")
            .start();

    // try to connect in standalone mode - should fail
    try {
      conn =
          DriverManager.getConnection("jdbc:h2:tcp://localhost:" + port1 + "/test", user, password);
      fail("should not be able to connect in standalone mode");
    } catch (SQLException e) {
      assertKnownException(e);
    }
    try {
      DriverManager.getConnection("jdbc:h2:tcp://localhost:" + port2 + "/test", user, password);
      fail("should not be able to connect in standalone mode");
    } catch (SQLException e) {
      assertKnownException(e);
    }

    // test a cluster connection
    conn = DriverManager.getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
    check(conn, len, "'" + serverList + "'");
    conn.close();

    // stop server 2, and test if only one server is available
    n2.stop();
    conn = DriverManager.getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
    check(conn, len, "''");
    conn.close();
    conn = DriverManager.getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
    check(conn, len, "''");
    conn.close();

    // disable the cluster
    conn =
        DriverManager.getConnection(
            "jdbc:h2:tcp://localhost:" + port1 + "/test;CLUSTER=''", user, password);
    conn.close();
    n1.stop();

    // re-create the cluster
    DeleteDbFiles.main("-dir", getBaseDir() + "/node2", "-quiet");
    CreateCluster.main(
        "-urlSource",
        urlNode1,
        "-urlTarget",
        urlNode2,
        "-user",
        user,
        "-password",
        password,
        "-serverList",
        serverList);
    n1 =
        org.h2.tools.Server.createTcpServer(
                "-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1")
            .start();
    n2 =
        org.h2.tools.Server.createTcpServer(
                "-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2")
            .start();

    conn = DriverManager.getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
    stat = conn.createStatement();
    stat.execute("CREATE TABLE BOTH(ID INT)");

    n1.stop();

    stat.execute("CREATE TABLE A(ID INT)");
    conn.close();
    n2.stop();

    n1 =
        org.h2.tools.Server.createTcpServer(
                "-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1")
            .start();
    conn =
        DriverManager.getConnection(
            "jdbc:h2:tcp://localhost:" + port1 + "/test;CLUSTER=''", user, password);
    check(conn, len, "''");
    conn.close();
    n1.stop();

    n2 =
        org.h2.tools.Server.createTcpServer(
                "-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2")
            .start();
    conn =
        DriverManager.getConnection(
            "jdbc:h2:tcp://localhost:" + port2 + "/test;CLUSTER=''", user, password);
    check(conn, len, "''");
    conn.createStatement().execute("SELECT * FROM A");
    conn.close();
    n2.stop();
    deleteFiles();
  }