Search in sources :

Example 51 with Mode

use of org.h2.engine.Mode in project h2database by h2database.

the class TcpServer method stopServer.

/**
 * Stop a running server. This method is called via reflection from the
 * STOP_SERVER function.
 *
 * @param port the port where the server runs, or 0 for all running servers
 * @param password the password (or null)
 * @param shutdownMode the shutdown mode, SHUTDOWN_NORMAL or SHUTDOWN_FORCE.
 */
public static void stopServer(int port, String password, int shutdownMode) {
    if (port == 0) {
        for (int p : SERVERS.keySet().toArray(new Integer[0])) {
            if (p != 0) {
                stopServer(p, password, shutdownMode);
            }
        }
        return;
    }
    TcpServer server = SERVERS.get(port);
    if (server == null) {
        return;
    }
    if (!server.managementPassword.equals(password)) {
        return;
    }
    if (shutdownMode == SHUTDOWN_NORMAL) {
        server.stopManagementDb();
        server.stop = true;
        try {
            Socket s = NetUtils.createLoopbackSocket(port, false);
            s.close();
        } catch (Exception e) {
        // try to connect - so that accept returns
        }
    } else if (shutdownMode == SHUTDOWN_FORCE) {
        server.stop();
    }
    server.shutdown();
}
Also used : Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) DbException(org.h2.message.DbException) UnknownHostException(java.net.UnknownHostException) SQLException(java.sql.SQLException)

Example 52 with Mode

use of org.h2.engine.Mode in project h2database by h2database.

the class TestCluster method testStartStopCluster.

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 = 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
    // should not be able to connect in standalone mode
    assertThrows(ErrorCode.CLUSTER_ERROR_DATABASE_RUNS_CLUSTERED_1, this).getConnection("jdbc:h2:tcp://localhost:" + port1 + "/test", user, password);
    assertThrows(ErrorCode.CLUSTER_ERROR_DATABASE_RUNS_CLUSTERED_1, this).getConnection("jdbc:h2:tcp://localhost:" + port2 + "/test", user, password);
    // test a cluster connection
    conn = 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 = getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
    check(conn, len, "''");
    conn.close();
    conn = getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
    check(conn, len, "''");
    conn.close();
    // disable the cluster
    conn = 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 = 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 = 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 = getConnection("jdbc:h2:tcp://localhost:" + port2 + "/test;CLUSTER=''", user, password);
    check(conn, len, "''");
    conn.createStatement().execute("SELECT * FROM A");
    conn.close();
    n2.stop();
    deleteFiles();
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 53 with Mode

use of org.h2.engine.Mode in project h2database by h2database.

the class TestCompatibilityOracle method testDate.

private void testDate() throws SQLException {
    deleteDb("oracle");
    Connection conn = getConnection("oracle;MODE=Oracle");
    Statement stat = conn.createStatement();
    Timestamp t1 = Timestamp.valueOf("2011-02-03 12:11:10");
    Timestamp t2 = Timestamp.valueOf("1999-10-15 13:14:15");
    Timestamp t3 = Timestamp.valueOf("2030-11-22 11:22:33");
    Timestamp t4 = Timestamp.valueOf("2018-01-10 22:10:01");
    stat.execute("CREATE TABLE TEST (ID INT PRIMARY KEY, D DATE)");
    stat.executeUpdate("INSERT INTO TEST VALUES(1, TIMESTAMP '2011-02-03 12:11:10')");
    stat.executeUpdate("INSERT INTO TEST VALUES(2, CAST ('1999-10-15 13:14:15' AS DATE))");
    stat.executeUpdate("INSERT INTO TEST VALUES(3, '2030-11-22 11:22:33')");
    PreparedStatement ps = conn.prepareStatement("INSERT INTO TEST VALUES (?, ?)");
    ps.setInt(1, 4);
    ps.setTimestamp(2, t4);
    ps.executeUpdate();
    ResultSet rs = stat.executeQuery("SELECT D FROM TEST ORDER BY ID");
    rs.next();
    assertEquals(t1, rs.getTimestamp(1));
    rs.next();
    assertEquals(t2, rs.getTimestamp(1));
    rs.next();
    assertEquals(t3, rs.getTimestamp(1));
    rs.next();
    assertEquals(t4, rs.getTimestamp(1));
    assertFalse(rs.next());
    conn.close();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 54 with Mode

use of org.h2.engine.Mode in project h2database by h2database.

the class TestMode method testPostgresqlClientInfo.

private void testPostgresqlClientInfo() {
    Mode postgresqlMode = Mode.getInstance("PostgreSQL");
    assertTrue(postgresqlMode.supportedClientInfoPropertiesRegEx.matcher("ApplicationName").matches());
    assertFalse(postgresqlMode.supportedClientInfoPropertiesRegEx.matcher("AnyOtherValue").matches());
}
Also used : Mode(org.h2.engine.Mode)

Example 55 with Mode

use of org.h2.engine.Mode in project h2database by h2database.

the class TestPattern method testPattern.

private void testPattern() {
    CompareMode mode = CompareMode.getInstance(null, 0);
    CompareLike comp = new CompareLike(mode, "\\", null, null, null, false);
    test(comp, "B", "%_");
    test(comp, "A", "A%");
    test(comp, "A", "A%%");
    test(comp, "A_A", "%\\_%");
    for (int i = 0; i < 10000; i++) {
        String pattern = getRandomPattern();
        String value = getRandomValue();
        test(comp, value, pattern);
    }
}
Also used : CompareMode(org.h2.value.CompareMode) CompareLike(org.h2.expression.CompareLike)

Aggregations

Connection (java.sql.Connection)12 Mode (org.h2.engine.Mode)12 ValueString (org.h2.value.ValueString)11 PreparedStatement (java.sql.PreparedStatement)9 Column (org.h2.table.Column)9 IOException (java.io.IOException)8 Statement (java.sql.Statement)8 ArrayList (java.util.ArrayList)8 DbException (org.h2.message.DbException)8 CompareMode (org.h2.value.CompareMode)8 Value (org.h2.value.Value)8 ResultSet (java.sql.ResultSet)7 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)7 SQLException (java.sql.SQLException)6 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)6 ValueExpression (org.h2.expression.ValueExpression)5 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)5 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)4 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)4 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)4