Search in sources :

Example 41 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestMultiThread method testConcurrentView.

private void testConcurrentView() throws Exception {
    if (config.mvcc || config.mvStore) {
        return;
    }
    String db = getTestName();
    deleteDb(db);
    final String url = getURL(db + ";MULTI_THREADED=1", true);
    final Random r = new Random();
    try (Connection conn = getConnection(url)) {
        Statement stat = conn.createStatement();
        StringBuilder buff = new StringBuilder();
        buff.append("create table test(id int");
        final int len = 3;
        for (int i = 0; i < len; i++) {
            buff.append(", x" + i + " int");
        }
        buff.append(")");
        stat.execute(buff.toString());
        stat.execute("create view test_view as select * from test");
        stat.execute("insert into test(id) select x from system_range(1, 2)");
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                Connection c2 = getConnection(url);
                while (!stop) {
                    c2.prepareStatement("select * from test_view where x" + r.nextInt(len) + "=1");
                }
                c2.close();
            }
        };
        t.execute();
        SynchronizedVerifier.setDetect(SmallLRUCache.class, true);
        for (int i = 0; i < 1000; i++) {
            conn.prepareStatement("select * from test_view where x" + r.nextInt(len) + "=1");
        }
        t.get();
        SynchronizedVerifier.setDetect(SmallLRUCache.class, false);
    }
}
Also used : Task(org.h2.util.Task) Random(java.util.Random) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 42 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestOpenClose method testCase.

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() {

            @Override
            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++) {
        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();
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 43 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestRunscript method testCancelScript.

private void testCancelScript() throws Exception {
    if (config.travis) {
        // fails regularly under Travis, not sure why
        return;
    }
    deleteDb("runscript");
    Connection conn;
    conn = getConnection("runscript");
    final Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key) as " + "select x from system_range(1, 20000)");
    stat.execute("script simple drop to '" + getBaseDir() + "/backup.sql'");
    stat.execute("set throttle 1000");
    // need to wait a bit (throttle is only used every 50 ms)
    Thread.sleep(200);
    final String dir = getBaseDir();
    Task task;
    task = new Task() {

        @Override
        public void call() throws SQLException {
            stat.execute("script simple drop to '" + dir + "/backup2.sql'");
        }
    };
    task.execute();
    Thread.sleep(200);
    stat.cancel();
    SQLException e = (SQLException) task.getException();
    assertNotNull(e);
    assertEquals(ErrorCode.STATEMENT_WAS_CANCELED, e.getErrorCode());
    stat.execute("set throttle 1000");
    // need to wait a bit (throttle is only used every 50 ms)
    Thread.sleep(100);
    task = new Task() {

        @Override
        public void call() throws SQLException {
            stat.execute("runscript from '" + dir + "/backup.sql'");
        }
    };
    task.execute();
    Thread.sleep(200);
    stat.cancel();
    e = (SQLException) task.getException();
    assertNotNull(e);
    assertEquals(ErrorCode.STATEMENT_WAS_CANCELED, e.getErrorCode());
    conn.close();
    FileUtils.delete(getBaseDir() + "/backup.sql");
    FileUtils.delete(getBaseDir() + "/backup2.sql");
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection)

Example 44 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestAll method runAddedTests.

private void runAddedTests(int threadCount) {
    Task[] tasks = new Task[threadCount];
    for (int i = 0; i < threadCount; i++) {
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                while (true) {
                    TestBase test;
                    synchronized (tests) {
                        if (tests.isEmpty()) {
                            break;
                        }
                        test = tests.remove(0);
                    }
                    test.runTest(TestAll.this);
                }
            }
        };
        t.execute();
        tasks[i] = t;
    }
    for (Task t : tasks) {
        t.get();
    }
}
Also used : Task(org.h2.util.Task) TimerTask(java.util.TimerTask) TestCheckpoint(org.h2.test.db.TestCheckpoint)

Example 45 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestFullText method testMultiThreaded.

private void testMultiThreaded(boolean lucene) throws Exception {
    final String prefix = lucene ? "FTL" : "FT";
    trace("Testing multithreaded " + prefix);
    deleteDb("fullText");
    ArrayList<Connection> connList = new ArrayList<>();
    try {
        int len = 2;
        Task[] task = new Task[len];
        for (int i = 0; i < len; i++) {
            final Connection conn = getConnection("fullText;LOCK_TIMEOUT=60000", connList);
            Statement stat = conn.createStatement();
            initFullText(stat, lucene);
            initFullText(stat, lucene);
            final String tableName = "TEST" + i;
            stat.execute("CREATE TABLE " + tableName + "(ID INT PRIMARY KEY, DATA VARCHAR)");
            stat.execute("CALL " + prefix + "_CREATE_INDEX('PUBLIC', '" + tableName + "', NULL)");
            task[i] = new Task() {

                @Override
                public void call() throws SQLException {
                    trace("starting thread " + Thread.currentThread());
                    PreparedStatement prep = conn.prepareStatement("INSERT INTO " + tableName + " VALUES(?, ?)");
                    Statement stat = conn.createStatement();
                    Random random = new Random();
                    int x = 0;
                    while (!stop) {
                        trace("stop = " + stop + " for " + Thread.currentThread());
                        StringBuilder buff = new StringBuilder();
                        for (int j = 0; j < 1000; j++) {
                            buff.append(" ").append(random.nextInt(10000));
                            buff.append(" x").append(j);
                            buff.append(" ").append(KNOWN_WORDS[j % KNOWN_WORDS.length]);
                        }
                        prep.setInt(1, x);
                        prep.setString(2, buff.toString());
                        prep.execute();
                        x++;
                        for (String knownWord : KNOWN_WORDS) {
                            trace("searching for " + knownWord + " with " + Thread.currentThread());
                            ResultSet rs = stat.executeQuery("SELECT * FROM " + prefix + "_SEARCH('" + knownWord + "', 0, 0)");
                            assertTrue(rs.next());
                        }
                    }
                    trace("closing connection");
                    if (!config.memory) {
                        conn.close();
                    }
                    trace("completed thread " + Thread.currentThread());
                }
            };
        }
        for (Task t : task) {
            t.execute();
        }
        trace("sleeping");
        Thread.sleep(1000);
        trace("setting stop to true");
        for (Task t : task) {
            trace("joining " + t);
            t.get();
            trace("done joining " + t);
        }
    } finally {
        close(connList);
    }
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Random(java.util.Random) ResultSet(java.sql.ResultSet)

Aggregations

Task (org.h2.util.Task)71 Connection (java.sql.Connection)33 Statement (java.sql.Statement)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 PreparedStatement (java.sql.PreparedStatement)22 SQLException (java.sql.SQLException)22 MVStore (org.h2.mvstore.MVStore)20 Random (java.util.Random)18 ResultSet (java.sql.ResultSet)14 JdbcConnection (org.h2.jdbc.JdbcConnection)7 IOException (java.io.IOException)5 ServerSocket (java.net.ServerSocket)5 Socket (java.net.Socket)5 ConcurrentModificationException (java.util.ConcurrentModificationException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 SSLServerSocket (javax.net.ssl.SSLServerSocket)4 SSLSocket (javax.net.ssl.SSLSocket)4 OutputStream (java.io.OutputStream)3 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3