Search in sources :

Example 31 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestCluster method testRecover.

private void testRecover() throws SQLException {
    if (config.memory || config.networked || config.cipher != null) {
        return;
    }
    deleteFiles();
    org.h2.Driver.load();
    String user = getUser(), password = getPassword();
    Connection conn;
    Statement stat;
    ResultSet rs;
    Server server1 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node1").start();
    int port1 = server1.getPort();
    Server server2 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node2").start();
    int port2 = server2.getPort();
    String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", true);
    String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", true);
    String serverList = "localhost:" + port1 + ",localhost:" + port2;
    String urlCluster = getURL("jdbc:h2:tcp://" + serverList + "/test", true);
    CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
    conn = getConnection(urlCluster, user, password);
    stat = conn.createStatement();
    stat.execute("create table t1(id int, name varchar(30))");
    stat.execute("insert into t1 values(1, 'a'), (2, 'b'), (3, 'c')");
    rs = stat.executeQuery("select count(*) from t1");
    rs.next();
    assertEquals(3, rs.getInt(1));
    server2.stop();
    DeleteDbFiles.main("-dir", getBaseDir() + "/node2", "-quiet");
    stat.execute("insert into t1 values(4, 'd'), (5, 'e')");
    rs = stat.executeQuery("select count(*) from t1");
    rs.next();
    assertEquals(5, rs.getInt(1));
    server2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start();
    CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
    conn.close();
    conn = getConnection(urlCluster, user, password);
    stat = conn.createStatement();
    rs = stat.executeQuery("select count(*) from t1");
    rs.next();
    assertEquals(5, rs.getInt(1));
    conn.close();
    server1.stop();
    server2.stop();
    deleteFiles();
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 32 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestCluster method testClientInfo.

private void testClientInfo() throws SQLException {
    if (config.memory || config.networked || config.cipher != null) {
        return;
    }
    deleteFiles();
    org.h2.Driver.load();
    String user = getUser(), password = getPassword();
    Connection conn;
    Server n1 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node1").start();
    int port1 = n1.getPort();
    Server n2 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node2").start();
    int port2 = n2.getPort();
    String serverList = "localhost:" + port1 + ",localhost:" + port2;
    String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", true);
    String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", true);
    String urlCluster = getURL("jdbc:h2:tcp://" + serverList + "/test", true);
    CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
    conn = getConnection(urlCluster, user, password);
    Properties p = conn.getClientInfo();
    assertEquals("2", p.getProperty("numServers"));
    assertEquals("127.0.0.1:" + port1, p.getProperty("server0"));
    assertEquals("127.0.0.1:" + port2, p.getProperty("server1"));
    assertEquals("2", conn.getClientInfo("numServers"));
    assertEquals("127.0.0.1:" + port1, conn.getClientInfo("server0"));
    assertEquals("127.0.0.1:" + port2, conn.getClientInfo("server1"));
    conn.close();
    // stop server 2, and test if only one server is available
    n2.stop();
    conn = getConnection(urlCluster, user, password);
    p = conn.getClientInfo();
    assertEquals("1", p.getProperty("numServers"));
    assertEquals("127.0.0.1:" + port1, p.getProperty("server0"));
    assertEquals("1", conn.getClientInfo("numServers"));
    assertEquals("127.0.0.1:" + port1, conn.getClientInfo("server0"));
    conn.close();
    n1.stop();
    deleteFiles();
}
Also used : Server(org.h2.tools.Server) Connection(java.sql.Connection) Properties(java.util.Properties)

Example 33 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestCluster method testClob.

private void testClob() throws SQLException {
    if (config.memory || config.networked || config.cipher != null) {
        return;
    }
    deleteFiles();
    org.h2.Driver.load();
    String user = getUser(), password = getPassword();
    Connection conn;
    Statement stat;
    Server n1 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node1").start();
    int port1 = n1.getPort();
    String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", false);
    conn = getConnection(url1, user, password);
    stat = conn.createStatement();
    stat.execute("create table t1(id int, name clob)");
    stat.execute("insert into t1 values(1, repeat('Hello', 50))");
    conn.close();
    Server n2 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node2").start();
    int port2 = n2.getPort();
    String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", false);
    String serverList = "localhost:" + port1 + ",localhost:" + port2;
    String urlCluster = getURL("jdbc:h2:tcp://" + serverList + "/test", true);
    CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
    conn = getConnection(urlCluster, user, password);
    conn.close();
    n1.stop();
    n2.stop();
    deleteFiles();
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 34 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestCluster method testCase.

private void testCase() throws SQLException {
    if (config.memory || config.networked || config.cipher != null) {
        return;
    }
    deleteFiles();
    org.h2.Driver.load();
    String user = getUser(), password = getPassword();
    Connection conn;
    Statement stat;
    ResultSet rs;
    Server n1 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node1").start();
    int port1 = n1.getPort();
    Server n2 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node2").start();
    int port2 = n2.getPort();
    String serverList = "localhost:" + port1 + ",localhost:" + port2;
    String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", true);
    String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", true);
    String urlCluster = getURL("jdbc:h2:tcp://" + serverList + "/test", true);
    CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
    conn = getConnection(urlCluster, user, password);
    stat = conn.createStatement();
    assertTrue(conn.getAutoCommit());
    stat.execute("create table test(name int)");
    assertTrue(conn.getAutoCommit());
    stat.execute("insert into test values(1)");
    conn.setAutoCommit(false);
    assertFalse(conn.getAutoCommit());
    stat.execute("insert into test values(2)");
    assertFalse(conn.getAutoCommit());
    conn.rollback();
    rs = stat.executeQuery("select * from test order by name");
    assertTrue(rs.next());
    assertFalse(rs.next());
    conn.close();
    // stop server 2, and test if only one server is available
    n2.stop();
    conn = getConnection(urlCluster, user, password);
    stat = conn.createStatement();
    rs = stat.executeQuery("select * from test");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    conn.close();
    n1.stop();
    deleteFiles();
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 35 with Server

use of org.h2.tools.Server in project h2database by h2database.

the class TestReadOnly method testReadOnlyInZip.

private void testReadOnlyInZip() throws SQLException {
    if (config.cipher != null) {
        return;
    }
    deleteDb("readonlyInZip");
    String dir = getBaseDir();
    Connection conn = getConnection("readonlyInZip");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID INT) AS " + "SELECT X FROM SYSTEM_RANGE(1, 20)");
    conn.close();
    Backup.execute(dir + "/readonly.zip", dir, "readonlyInZip", true);
    conn = getConnection("jdbc:h2:zip:" + dir + "/readonly.zip!/readonlyInZip", getUser(), getPassword());
    conn.createStatement().execute("select * from test where id=1");
    conn.close();
    Server server = Server.createTcpServer("-baseDir", dir);
    server.start();
    int port = server.getPort();
    try {
        conn = getConnection("jdbc:h2:tcp://localhost:" + port + "/zip:readonly.zip!/readonlyInZip", getUser(), getPassword());
        conn.createStatement().execute("select * from test where id=1");
        conn.close();
        FilePathZip2.register();
        conn = getConnection("jdbc:h2:tcp://localhost:" + port + "/zip2:readonly.zip!/readonlyInZip", getUser(), getPassword());
        conn.createStatement().execute("select * from test where id=1");
        conn.close();
    } finally {
        server.stop();
    }
    deleteDb("readonlyInZip");
}
Also used : Server(org.h2.tools.Server) Statement(java.sql.Statement) Connection(java.sql.Connection)

Aggregations

Server (org.h2.tools.Server)47 Connection (java.sql.Connection)27 SQLException (java.sql.SQLException)22 Statement (java.sql.Statement)17 PreparedStatement (java.sql.PreparedStatement)15 IOException (java.io.IOException)13 ResultSet (java.sql.ResultSet)10 DbException (org.h2.message.DbException)9 Socket (java.net.Socket)8 Test (org.junit.Test)8 Properties (java.util.Properties)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)6 ServerSocket (java.net.ServerSocket)6 PrintStream (java.io.PrintStream)5 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)5 Task (org.h2.util.Task)5 TcpServer (org.h2.server.TcpServer)4 PgServer (org.h2.server.pg.PgServer)4 WebServer (org.h2.server.web.WebServer)4