Package org.h2.util

Examples of org.h2.util.Task


    }

    private void testMultiThreaded() throws Exception {
        Task[] tasks = new Task[3];
        for (int i = 0; i < tasks.length; i++) {
            Task t = new Task() {
                public void call() {
                    CompressTool tool = CompressTool.getInstance();
                    byte[] buff = new byte[1024];
                    Random r = new Random();
                    while (!stop) {
                        r.nextBytes(buff);
                        byte[] test = tool.expand(tool.compress(buff, "LZF"));
                        assertEquals(buff, test);
                    }
                }
            };
            tasks[i] = t;
            t.execute();
        }
        Thread.sleep(1000);
        for (Task t : tasks) {
            t.get();
        }
    }
View Full Code Here


            System.setProperty(SysProperties.H2_BROWSER, "call:" + TestWeb.class.getName() + ".openBrowser");
            Server.openBrowser("testUrl");
            assertEquals("testUrl", lastUrl);
            String oldUrl = lastUrl;
            final Connection conn = getConnection("testWeb");
            Task t = new Task() {
                public void call() throws Exception {
                    Server.startWebServer(conn);
                }
            };
            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 (Exception e) {
                // the server stops on logout
            }
            t.get();
        } finally {
            if (old != null) {
                System.setProperty(SysProperties.H2_BROWSER, old);
            } else {
                System.clearProperty(SysProperties.H2_BROWSER);
View Full Code Here

        PipedOutputStream out = new PipedOutputStream(testIn);
        toolOut = new PrintStream(out, true);
        out = new PipedOutputStream();
        testOut = new PrintStream(out, true);
        toolIn = new PipedInputStream(out);
        Task task = new Task() {
            public void call() throws Exception {
                try {
                    Shell shell = new Shell();
                    shell.setIn(toolIn);
                    shell.setOut(toolOut);
                    shell.setErr(toolOut);
                    if (commandLineArgs) {
                        shell.runTool("-url", "jdbc:h2:mem:",
                                "-user", "sa", "-password", "sa");
                    } else {
                        shell.runTool();
                    }
                } finally {
                    toolOut.close();
                }
            }
        };
        task.execute();
        InputStreamReader reader = new InputStreamReader(testIn);
        lineReader = new LineNumberReader(reader);
        read("");
        read("Welcome to H2 Shell");
        read("Exit with");
        if (!commandLineArgs) {
            read("[Enter]");
            testOut.println("jdbc:h2:mem:");
            read("URL");
            testOut.println("");
            read("Driver");
            testOut.println("sa");
            read("User");
            testOut.println("sa");
            read("Password");
        }
        read("Commands are case insensitive");
        read("help or ?");
        read("list");
        read("maxwidth");
        read("autocommit");
        read("history");
        read("quit or exit");
        read("");
        testOut.println("history");
        read("sql> No history");
        testOut.println("1");
        read("sql> Not found");
        testOut.println("select 1 a;");
        read("sql> A");
        read("1");
        read("(1 row,");
        testOut.println("history");
        read("sql> #1: select 1 a");
        read("To re-run a statement, type the number and press and enter");
        testOut.println("1");
        read("sql> select 1 a");
        read("A");
        read("1");
        read("(1 row,");

        testOut.println("select 'x' || space(1000) large, 'y' small;");
        read("sql> LARGE");
        read("x");
        read("(data is partially truncated)");
        read("(1 row,");

        testOut.println("select x, 's' s from system_range(0, 10001);");
        read("sql> X    | S");
        for (int i = 0; i < 10000; i++) {
            read((i + "     ").substring(0, 4) + " | s");
        }
        for (int i = 10000; i <= 10001; i++) {
            read((i + "     ").substring(0, 5) + " | s");
        }
        read("(10002 rows,");

        testOut.println("select error;");
        read("sql> Error:");
        if (read("").startsWith("Column \"ERROR\" not found")) {
            read("");
        }
        testOut.println("create table test(id int primary key, name varchar)\n;");
        read("sql> ...>");
        testOut.println("insert into test values(1, 'Hello');");
        read("sql>");
        testOut.println("select null n, * from test;");
        read("sql> N    | ID | NAME");
        read("null | 1  | Hello");
        read("(1 row,");

        // test history
        for (int i = 0; i < 30; i++) {
            testOut.println("select " + i + " ID from test;");
            read("sql> ID");
            read("" + i);
            read("(1 row,");
        }
        testOut.println("20");
        read("sql> select 10 ID from test");
        read("ID");
        read("10");
        read("(1 row,");

        testOut.println("maxwidth");
        read("sql> Usage: maxwidth <integer value>");
        read("Maximum column width is now 100");
        testOut.println("maxwidth 80");
        read("sql> Maximum column width is now 80");
        testOut.println("autocommit");
        read("sql> Usage: autocommit [true|false]");
        read("Autocommit is now true");
        testOut.println("autocommit false");
        read("sql> Autocommit is now false");
        testOut.println("autocommit true");
        read("sql> Autocommit is now true");
        testOut.println("\n;");
        read("sql>");
        testOut.println("list");
        read("sql> Result list mode is now on");

        testOut.println("select 1 first, 2 second;");
        read("sql> FIRST : 1");
        read("SECOND: 2");
        read("(1 row, ");

        testOut.println("select x from system_range(1, 3);");
        read("sql> X: 1");
        read("");
        read("X: 2");
        read("");
        read("X: 3");
        read("(3 rows, ");

        testOut.println("select x, 2 as y from system_range(1, 3) where 1 = 0;");
        read("sql> X");
        read("Y");
        read("(0 rows, ");

        testOut.println("list");
        read("sql> Result list mode is now off");
        testOut.println("help");
        read("sql> Commands are case insensitive");
        read("help or ?");
        read("list");
        read("maxwidth");
        read("autocommit");
        read("history");
        read("quit or exit");
        read("");
        testOut.println("exit");
        read("sql>");
        task.get();
    }
View Full Code Here

        final Statement stat2 = conn2.createStatement();
        stat2.execute("set lock_timeout 1000");
        stat.execute("create table test(id int primary key, name varchar)");
        conn.setAutoCommit(false);
        final AtomicBoolean committed = new AtomicBoolean(false);
        Task t = new Task() {
            public void call() throws SQLException {
                try {
//System.out.println("insert2 hallo");
                    stat2.execute("insert into test values(0, 'Hallo')");
//System.out.println("insert2 hallo done");
                } catch (SQLException e) {
//System.out.println("insert2 hallo e " + e);
                    if (!committed.get()) {
                        throw e;
                    }
                }
            }
        };
//System.out.println("insert hello");
        stat.execute("insert into test values(0, 'Hello')");
        t.execute();
        Thread.sleep(500);
//System.out.println("insert hello commit");
        committed.set(true);
        conn.commit();
        t.get();
        ResultSet rs;
        rs = stat.executeQuery("select name from test");
        rs.next();
        assertEquals("Hello", rs.getString(1));
        stat.execute("drop table test");
View Full Code Here

        final Statement stat2 = conn2.createStatement();
        stat2.execute("set lock_timeout 1000");
        stat.execute("create table test(id int primary key, name varchar)");
        stat.execute("insert into test values(0, 'Hello')");
        conn.setAutoCommit(false);
        Task t = new Task() {
            public void call() throws SQLException {
                stat2.execute("update test set name = 'Hallo'");
            }
        };
        stat.execute("update test set name = 'Hi'");
        t.execute();
        Thread.sleep(500);
        conn.commit();
        t.get();
        ResultSet rs;
        rs = stat.executeQuery("select name from test");
        rs.next();
        assertEquals("Hallo", rs.getString(1));
        stat.execute("drop table test");
View Full Code Here

        Task[] tasks = new Task[len];
        final boolean[] stop = { false };
        for (int i = 0; i < len; i++) {
            final Connection c = connList[i];
            c.setAutoCommit(false);
            tasks[i] = new Task() {
                public void call() throws Exception {
                    while (!stop) {
                        c.createStatement().execute("merge into test values(1, 'x')");
                        c.commit();
                        Thread.sleep(1);
View Full Code Here

        final CountDownLatch latch = new CountDownLatch(len);

        for (int i = 0; i < len; i++) {
            final int x = i;
            tasks[i] = new Task() {
                public void call() throws Exception {
                    for (int a = 0; a < count; a++) {
                        connList[x].createStatement().execute("update test set value=value+1");
                        latch.countDown();
                        latch.await();
View Full Code Here

        stat.execute("create table test(id int primary key, data clob)");
        stat.execute("insert into test values(0, space(10000))");
        stat.execute("insert into test values(1, space(10001))");
        Task[] tasks = new Task[2];
        for (int i = 0; i < tasks.length; i++) {
            tasks[i] = new Task() {
                public void call() throws Exception {
                    PreparedStatement prep = conn.prepareStatement("select * from test where id = ?");
                    while (!stop) {
                        int x = (int) (Math.random() * 2);
                        prep.setInt(1, x);
View Full Code Here

        assertThrows(IllegalArgumentException.class, man).
                setMaxConnections(-1);
        man.setMaxConnections(2);
        // connection 1 (of 2)
        Connection conn = man.getConnection();
        Task t = new Task() {
            public void call() {
                while (!stop) {
                    // this calls notifyAll
                    man.setMaxConnections(1);
                    man.setMaxConnections(2);
                }
            }
        };
        t.execute();
        long time = System.currentTimeMillis();
        try {
            // connection 2 (of 1 or 2) may fail
            man.getConnection();
            // connection 3 (of 1 or 2) must fail
            man.getConnection();
            fail();
        } catch (SQLException e) {
            assertTrue(e.toString().toLowerCase().indexOf("timeout") >= 0);
            time = System.currentTimeMillis() - time;
            assertTrue("timeout after " + time + " ms", time > 1000);
        } finally {
            conn.close();
            t.get();
        }

        man.dispose();
    }
View Full Code Here

        stat.execute("create trigger test_u before update on test2 " +
                "for each row call \"" + DeleteTrigger.class.getName() + "\"");
        conn.setAutoCommit(false);
        conn2.setAutoCommit(false);
        stat2.execute("update test set id = 2");
        Task task = new Task() {
            public void call() throws Exception {
                Thread.sleep(300);
                stat2.execute("update test2 set id = 4");
            }
        };
        task.execute();
        Thread.sleep(100);
        try {
            stat.execute("update test2 set id = 3");
            task.get();
        } catch (SQLException e) {
            assertEquals(ErrorCode.LOCK_TIMEOUT_1, e.getErrorCode());
        }
        conn2.rollback();
        conn.rollback();
View Full Code Here

TOP

Related Classes of org.h2.util.Task

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.