Search in sources :

Example 26 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestOldVersion method testOldClientNewServer.

private void testOldClientNewServer() throws Exception {
    Server server = org.h2.tools.Server.createTcpServer();
    server.start();
    int port = server.getPort();
    assertThrows(ErrorCode.DRIVER_VERSION_ERROR_2, driver).connect("jdbc:h2:tcp://localhost:" + port + "/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", "" + port } });
    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);
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Method(java.lang.reflect.Method)

Example 27 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestCompress method testMultiThreaded.

private void testMultiThreaded() throws Exception {
    Task[] tasks = new Task[3];
    for (int i = 0; i < tasks.length; i++) {
        Task t = new Task() {

            @Override
            public void call() {
                CompressTool tool = CompressTool.getInstance();
                byte[] b = new byte[1024];
                Random r = new Random();
                while (!stop) {
                    r.nextBytes(b);
                    byte[] test = tool.expand(tool.compress(b, "LZF"));
                    assertEquals(b, test);
                }
            }
        };
        tasks[i] = t;
        t.execute();
    }
    Thread.sleep(1000);
    for (Task t : tasks) {
        t.get();
    }
}
Also used : Task(org.h2.util.Task) Random(java.util.Random) CompressTool(org.h2.tools.CompressTool)

Example 28 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestFileLockSerialized method testTwoWriters.

private void testTwoWriters() throws Exception {
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
    final String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
    Connection conn = getConnection(writeUrl, "sa", "sa");
    conn.createStatement().execute("create table test(id identity) as " + "select x from system_range(1, 100)");
    conn.close();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Thread.sleep(10);
                Connection c = getConnection(writeUrl, "sa", "sa");
                c.createStatement().execute("select * from test");
                c.close();
            }
        }
    }.execute();
    Thread.sleep(20);
    for (int i = 0; i < 2; i++) {
        conn = getConnection(writeUrl, "sa", "sa");
        Statement stat = conn.createStatement();
        stat.execute("drop table test");
        stat.execute("create table test(id identity) as " + "select x from system_range(1, 100)");
        conn.createStatement().execute("select * from test");
        conn.close();
    }
    Thread.sleep(100);
    conn = getConnection(writeUrl, "sa", "sa");
    conn.createStatement().execute("select * from test");
    conn.close();
    task.get();
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) SQLException(java.sql.SQLException)

Example 29 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestWeb method testStartWebServerWithConnection.

private void testStartWebServerWithConnection() throws Exception {
    String old = System.getProperty(SysProperties.H2_BROWSER);
    try {
        System.setProperty(SysProperties.H2_BROWSER, "call:" + TestWeb.class.getName() + ".openBrowser");
        Server.openBrowser("testUrl");
        assertEquals("testUrl", lastUrl);
        String oldUrl = lastUrl;
        final Connection conn = getConnection(getTestName());
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                Server.startWebServer(conn, true);
            }
        };
        t.execute();
        for (int i = 0; lastUrl == oldUrl; i++) {
            if (i > 100) {
                throw new Exception("Browser not started");
            }
            Thread.sleep(100);
        }
        String url = lastUrl;
        WebClient client = new WebClient();
        client.readSessionId(url);
        url = client.getBaseUrl(url);
        try {
            client.get(url, "logout.do");
        } catch (ConnectException e) {
        // the server stops on logout
        }
        t.get();
        conn.close();
    } finally {
        if (old != null) {
            System.setProperty(SysProperties.H2_BROWSER, old);
        } else {
            System.clearProperty(SysProperties.H2_BROWSER);
        }
    }
}
Also used : Task(org.h2.util.Task) Connection(java.sql.Connection) ServletException(javax.servlet.ServletException) SQLException(java.sql.SQLException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConnectException(java.net.ConnectException)

Example 30 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestBenchmark method testConcurrency.

private void testConcurrency(String fileName, int concurrency, final int count) throws Exception {
    Thread.sleep(1000);
    final MVStore store = new MVStore.Builder().cacheSize(256).cacheConcurrency(concurrency).fileName(fileName).open();
    int threadCount = 128;
    final CountDownLatch wait = new CountDownLatch(1);
    final AtomicInteger counter = new AtomicInteger();
    final AtomicBoolean stopped = new AtomicBoolean();
    Task[] tasks = new Task[threadCount];
    // Profiler prof = new Profiler().startCollecting();
    for (int i = 0; i < threadCount; i++) {
        final int x = i;
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                MVMap<Integer, byte[]> map = store.openMap("test");
                Random random = new Random(x);
                wait.await();
                while (!stopped.get()) {
                    int key = random.nextInt(count);
                    byte[] data = map.get(key);
                    if (data.length > 1) {
                        counter.incrementAndGet();
                    }
                }
            }
        };
        t.execute("t" + i);
        tasks[i] = t;
    }
    wait.countDown();
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    stopped.set(true);
    for (Task t : tasks) {
        t.get();
    }
    // System.out.println(prof.getTop(5));
    String msg = "concurrency " + concurrency + " threads " + threadCount + " requests: " + counter;
    System.out.println(msg);
    trace(msg);
    store.close();
}
Also used : Task(org.h2.util.Task) CountDownLatch(java.util.concurrent.CountDownLatch) MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

Task (org.h2.util.Task)69 Connection (java.sql.Connection)68 Statement (java.sql.Statement)64 PreparedStatement (java.sql.PreparedStatement)60 ResultSet (java.sql.ResultSet)48 SQLException (java.sql.SQLException)42 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 SimpleResultSet (org.h2.tools.SimpleResultSet)24 MVStore (org.h2.mvstore.MVStore)20 Random (java.util.Random)19 JdbcConnection (org.h2.jdbc.JdbcConnection)19 CallableStatement (java.sql.CallableStatement)14 DbException (org.h2.message.DbException)13 IOException (java.io.IOException)10 JdbcSQLException (org.h2.jdbc.JdbcSQLException)7 ArrayList (java.util.ArrayList)6 Expression (org.h2.expression.Expression)6 ValueString (org.h2.value.ValueString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStream (java.io.OutputStream)4