private void testCalled() throws SQLException {
   Properties p = new Properties();
   p.setProperty("user", "sa");
   p.setProperty("password", "sa");
   calledOpened = false;
   calledClosingDatabase = false;
   p.put("DATABASE_EVENT_LISTENER", getClass().getName());
   org.h2.Driver.load();
   String url = "jdbc:h2:mem:databaseEventListener";
   Connection conn = org.h2.Driver.load().connect(url, p);
   conn.close();
   assertTrue(calledOpened);
   assertTrue(calledClosingDatabase);
 }
Ejemplo n.º 2
0
  public void contextInitialized(ServletContextEvent servletContextEvent) {
    try {
      org.h2.Driver.load();

      // This will get the setting from a context-param in web.xml if defined:
      ServletContext servletContext = servletContextEvent.getServletContext();
      String url = getParameter(servletContext, "db.url", "jdbc:h2:~/test");
      String user = getParameter(servletContext, "db.user", "sa");
      String password = getParameter(servletContext, "db.password", "sa");

      // Start the server if configured to do so
      String serverParams = getParameter(servletContext, "db.tcpServer", null);
      if (serverParams != null) {
        String[] params = StringUtils.arraySplit(serverParams, ' ', true);
        server = Server.createTcpServer(params);
        server.start();
      }

      // To access the database in server mode, use the database URL:
      // jdbc:h2:tcp://localhost/~/test
      conn = DriverManager.getConnection(url, user, password);
      servletContext.setAttribute("connection", conn);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  private void testCloseLog0(boolean shutdown) throws SQLException {
    if (config.memory) {
      return;
    }
    deleteDb("databaseEventListener");
    String url = getURL("databaseEventListener", true);
    String user = getUser(), password = getPassword();
    Properties p = new Properties();
    p.setProperty("user", user);
    p.setProperty("password", password);
    Connection conn = DriverManager.getConnection(url, p);
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar)");
    stat.execute("insert into test select x, space(1000) from system_range(1,1000)");
    if (shutdown) {
      stat.execute("shutdown");
    }
    conn.close();

    calledOpened = false;
    calledScan = false;
    p.put("DATABASE_EVENT_LISTENER", getClass().getName());
    conn = org.h2.Driver.load().connect(url, p);
    conn.close();
    if (calledOpened) {
      assertTrue(!calledScan);
    }
  }
 private void testIndexNotRebuilt() throws SQLException {
   if (config.memory) {
     return;
   }
   deleteDb("databaseEventListener");
   String url = getURL("databaseEventListener", true);
   String user = getUser(), password = getPassword();
   Properties p = new Properties();
   p.setProperty("user", user);
   p.setProperty("password", password);
   Connection conn = DriverManager.getConnection(url, p);
   Statement stat = conn.createStatement();
   // the old.id index head is at position 0
   stat.execute("create table old(id identity) as select 1");
   // the test.id index head is at position 1
   stat.execute("create table test(id identity) as select 1");
   conn.close();
   conn = DriverManager.getConnection(url, p);
   stat = conn.createStatement();
   // free up space at position 0
   stat.execute("drop table old");
   // truncate, relocating to position 0
   stat.execute("truncate table test");
   stat.execute("insert into test select 1");
   conn.close();
   calledCreateIndex = false;
   p.put("DATABASE_EVENT_LISTENER", getClass().getName());
   conn = org.h2.Driver.load().connect(url, p);
   conn.close();
   assertTrue(!calledCreateIndex);
 }
Ejemplo n.º 5
0
  private void testCase() throws Exception {
    if (config.memory) {
      return;
    }

    org.h2.Driver.load();
    deleteDb("openClose");
    final String url = getURL("openClose;FILE_LOCK=NO", true);
    final String user = getUser(), password = getPassword();
    Connection conn = DriverManager.getConnection(url, user, password);
    conn.createStatement().execute("drop table employee if exists");
    conn.createStatement()
        .execute("create table employee(id int primary key, name varchar, salary int)");
    conn.close();
    // previously using getSize(200, 1000);
    // but for Ubuntu, the default ulimit is 1024,
    // which breaks the test
    int len = getSize(10, 50);
    Task[] tasks = new Task[len];
    for (int i = 0; i < len; i++) {
      tasks[i] =
          new Task() {
            public void call() throws SQLException {
              Connection c = DriverManager.getConnection(url, user, password);
              PreparedStatement prep = c.prepareStatement("insert into employee values(?, ?, 0)");
              int id = getNextId();
              prep.setInt(1, id);
              prep.setString(2, "employee " + id);
              prep.execute();
              c.close();
            }
          };
      tasks[i].execute();
    }
    // for(int i=0; i<len; i++) {
    // threads[i].start();
    // }
    for (int i = 0; i < len; i++) {
      tasks[i].get();
    }
    conn = DriverManager.getConnection(url, user, password);
    ResultSet rs = conn.createStatement().executeQuery("select count(*) from employee");
    rs.next();
    assertEquals(len, rs.getInt(1));
    conn.close();
  }
Ejemplo n.º 6
0
 public static synchronized FileSystemDatabase register(String url) {
   Connection conn;
   try {
     if (url.startsWith("jdbc:h2:")) {
       // avoid using DriverManager if possible
       conn = Driver.load().connect(url, new Properties());
     } else {
       conn = JdbcUtils.getConnection(null, url, new Properties());
     }
     boolean log = url.toUpperCase().indexOf("TRACE_") >= 0;
     FileSystemDatabase fs = new FileSystemDatabase(url, conn, log);
     FileSystem.register(fs);
     return fs;
   } catch (SQLException e) {
     throw new RuntimeException(e);
   }
 }
 @Override
 public void run() {
   try {
     org.h2.Driver.load();
     Connection conn =
         DriverManager.getConnection(
             url + ";MULTI_THREADED=1;LOCK_MODE=3;WRITE_DELAY=0", user, password);
     conn.createStatement()
         .execute(
             "CREATE TABLE TEST" + id + "(COL1 BIGINT AUTO_INCREMENT PRIMARY KEY, COL2 BIGINT)");
     PreparedStatement prep = conn.prepareStatement("insert into TEST" + id + "(col2) values (?)");
     for (int i = 0; !master.stop; i++) {
       prep.setLong(1, i);
       prep.execute();
     }
     conn.close();
   } catch (SQLException e) {
     e.printStackTrace();
   }
 }
 private void testIndexRebuiltOnce() throws SQLException {
   if (config.memory) {
     return;
   }
   deleteDb("databaseEventListener");
   String url = getURL("databaseEventListener", true);
   String user = getUser(), password = getPassword();
   Properties p = new Properties();
   p.setProperty("user", user);
   p.setProperty("password", password);
   Connection conn;
   Statement stat;
   conn = DriverManager.getConnection(url, p);
   stat = conn.createStatement();
   // the old.id index head is at position 0
   stat.execute("create table old(id identity) as select 1");
   // the test.id index head is at position 1
   stat.execute("create table test(id identity) as select 1");
   conn.close();
   conn = DriverManager.getConnection(url, p);
   stat = conn.createStatement();
   // free up space at position 0
   stat.execute("drop table old");
   stat.execute("insert into test values(2)");
   stat.execute("checkpoint sync");
   stat.execute("shutdown immediately");
   try {
     conn.close();
   } catch (SQLException e) {
     assertKnownException(e);
   }
   // now the index should be re-built
   conn = DriverManager.getConnection(url, p);
   conn.close();
   calledCreateIndex = false;
   p.put("DATABASE_EVENT_LISTENER", getClass().getName());
   conn = org.h2.Driver.load().connect(url, p);
   conn.close();
   assertTrue(!calledCreateIndex);
 }
Ejemplo n.º 9
0
  @Override
  public void test() throws Exception {
    org.h2.Driver.load();
    deleteDb("openClose");

    // int len = getSize(5, 100);
    int len = 10;
    TestMultiThread[] threads = new TestMultiThread[len];
    for (int i = 0; i < len; i++) {
      threads[i] = new TestMultiNews(this);
    }
    threads[0].first();
    for (int i = 0; i < len; i++) {
      threads[i].start();
    }
    Thread.sleep(10000);
    this.stop = true;
    for (int i = 0; i < len; i++) {
      threads[i].join();
    }
    threads[0].finalTest();
  }
Ejemplo n.º 10
0
  private void testOldClientNewServer() throws Exception {
    Server server = org.h2.tools.Server.createTcpServer("-tcpPort", "9001");
    server.start();
    assertThrows(ErrorCode.DRIVER_VERSION_ERROR_2, driver)
        .connect("jdbc:h2:tcp://localhost:9001/mem:test", null);
    server.stop();

    Class<?> serverClass = cl.loadClass("org.h2.tools.Server");
    Method m;
    m = serverClass.getMethod("createTcpServer", String[].class);
    Object serverOld = m.invoke(null, new Object[] {new String[] {"-tcpPort", "9001"}});
    m = serverOld.getClass().getMethod("start");
    m.invoke(serverOld);
    Connection conn;
    conn = org.h2.Driver.load().connect("jdbc:h2:mem:", null);
    Statement stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("call 1");
    rs.next();
    assertEquals(1, rs.getInt(1));
    conn.close();
    m = serverOld.getClass().getMethod("stop");
    m.invoke(serverOld);
  }
Ejemplo n.º 11
0
 private void testBackup() throws SQLException {
   if (config.memory) {
     return;
   }
   deleteDb("openClose");
   String url = getURL("openClose", true);
   org.h2.Driver.load();
   Connection conn = DriverManager.getConnection(url, "sa", "abc def");
   Statement stat = conn.createStatement();
   stat.execute("CREATE TABLE TEST(C CLOB)");
   stat.execute("INSERT INTO TEST VALUES(SPACE(10000))");
   stat.execute("BACKUP TO '" + getBaseDir() + "/test.zip'");
   conn.close();
   deleteDb("openClose");
   Restore.execute(getBaseDir() + "/test.zip", getBaseDir(), null, true);
   conn = DriverManager.getConnection(url, "sa", "abc def");
   stat = conn.createStatement();
   ResultSet rs = stat.executeQuery("SELECT * FROM TEST");
   rs.next();
   assertEquals(10000, rs.getString(1).length());
   assertFalse(rs.next());
   conn.close();
   FileUtils.delete(getBaseDir() + "/test.zip");
 }
Ejemplo n.º 12
0
 private static Connection getResultConnection() throws SQLException {
   org.h2.Driver.load();
   return DriverManager.getConnection("jdbc:h2:data/results");
 }
Ejemplo n.º 13
0
 static {
   org.h2.Driver.load();
 }
Ejemplo n.º 14
0
 Connection getConnection() throws SQLException {
   org.h2.Driver.load();
   String url = "jdbc:h2:" + getBaseDir() + "/halt";
   // String url = "jdbc:h2:" + baseDir + "/halt;TRACE_LEVEL_FILE=3";
   return DriverManager.getConnection(url, "sa", "sa");
 }
Ejemplo n.º 15
0
  private void testCreateClusterAtRuntime() throws SQLException {
    if (config.memory || config.networked || config.cipher != null) {
      return;
    }
    int port1 = 9191, port2 = 9192;
    String serverList = "localhost:" + port1 + ",localhost:" + port2;
    deleteFiles();

    org.h2.Driver.load();
    String user = getUser(), password = getPassword();
    Connection conn;
    Statement stat;
    String url1 = "jdbc:h2:tcp://localhost:" + port1 + "/test";
    String url2 = "jdbc:h2:tcp://localhost:" + port2 + "/test";
    String urlCluster = "jdbc:h2:tcp://" + serverList + "/test";
    int len = 10;

    // initialize the database
    Server n1 =
        org.h2.tools.Server.createTcpServer(
                "-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1")
            .start();
    conn = DriverManager.getConnection(url1, user, password);
    stat = conn.createStatement();
    stat.execute(
        "create table test(id int primary key, name varchar) as "
            + "select x, 'Data' || x from system_range(0, "
            + (len - 1)
            + ")");
    stat.execute("create user test password 'test'");
    stat.execute("grant all on test to test");

    // start the second server
    Server n2 =
        org.h2.tools.Server.createTcpServer(
                "-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2")
            .start();

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

    // check the original connection is closed
    try {
      stat.execute("select * from test");
      fail();
    } catch (SQLException e) {
      // expected
      JdbcUtils.closeSilently(conn);
    }

    // test the cluster connection
    Connection connApp =
        DriverManager.getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user, password);
    check(connApp, len, "'" + serverList + "'");

    // delete the rows, but don't commit
    connApp.setAutoCommit(false);
    connApp.createStatement().execute("delete from test");

    // stop server 2, and test if only one server is available
    n2.stop();

    // rollback the transaction
    connApp.createStatement().executeQuery("select count(*) from test");
    connApp.rollback();
    check(connApp, len, "''");
    connApp.setAutoCommit(true);

    // re-create the cluster
    n2 =
        org.h2.tools.Server.createTcpServer(
                "-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2")
            .start();
    CreateCluster.main(
        "-urlSource",
        url1,
        "-urlTarget",
        url2,
        "-user",
        user,
        "-password",
        password,
        "-serverList",
        serverList);

    // test the cluster connection
    check(connApp, len, "'" + serverList + "'");

    // test a non-admin user
    String user2 = "test", password2 = getPassword("test");
    connApp = DriverManager.getConnection(urlCluster, user2, password2);
    check(connApp, len, "'" + serverList + "'");

    n1.stop();

    // test non-admin cluster connection if only one server runs
    Connection connApp2 =
        DriverManager.getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2);
    check(connApp2, len, "''");
    connApp2.close();
    // test non-admin cluster connection if only one server runs
    connApp2 = DriverManager.getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2);
    check(connApp2, len, "''");
    connApp2.close();

    n2.stop();
    deleteFiles();
  }
Ejemplo n.º 16
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();
  }