Search in sources :

Example 66 with Task

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

the class TestConcurrent method testConcurrentStoreAndClose.

private void testConcurrentStoreAndClose() throws InterruptedException {
    String fileName = "memFS:" + getTestName();
    for (int i = 0; i < 10; i++) {
        FileUtils.delete(fileName);
        final MVStore s = openStore(fileName);
        try {
            final AtomicInteger counter = new AtomicInteger();
            Task task = new Task() {

                @Override
                public void call() throws Exception {
                    while (!stop) {
                        s.setStoreVersion(counter.incrementAndGet());
                        s.commit();
                    }
                }
            };
            task.execute();
            while (counter.get() < 5) {
                Thread.sleep(1);
            }
            try {
                s.close();
                // immediately)
                for (int x = counter.get(), y = x; x <= y + 2; x++) {
                    Thread.sleep(1);
                }
                Exception e = task.getException();
                assertEquals(DataUtils.ERROR_CLOSED, DataUtils.getErrorCode(e.getMessage()));
            } catch (IllegalStateException e) {
                // sometimes storing works, in which case
                // closing must fail
                assertEquals(DataUtils.ERROR_WRITING_FAILED, DataUtils.getErrorCode(e.getMessage()));
                task.get();
            }
        } finally {
            s.close();
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) Task(org.h2.util.Task) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentModificationException(java.util.ConcurrentModificationException)

Example 67 with Task

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

the class TestConcurrent method testConcurrentWrite.

private static void testConcurrentWrite(final AtomicInteger detected, final AtomicInteger notDetected) throws InterruptedException {
    final MVStore s = openStore(null);
    final MVMap<Integer, Integer> m = s.openMap("data");
    final int size = 20;
    final Random rand = new Random(1);
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                try {
                    if (rand.nextBoolean()) {
                        m.put(rand.nextInt(size), 1);
                    } else {
                        m.remove(rand.nextInt(size));
                    }
                    m.get(rand.nextInt(size));
                } catch (ConcurrentModificationException e) {
                    detected.incrementAndGet();
                } catch (NegativeArraySizeException e) {
                    notDetected.incrementAndGet();
                } catch (ArrayIndexOutOfBoundsException e) {
                    notDetected.incrementAndGet();
                } catch (IllegalArgumentException e) {
                    notDetected.incrementAndGet();
                } catch (NullPointerException e) {
                    notDetected.incrementAndGet();
                }
            }
        }
    };
    task.execute();
    try {
        Thread.sleep(1);
        for (int j = 0; j < 10; j++) {
            for (int i = 0; i < 10; i++) {
                try {
                    if (rand.nextBoolean()) {
                        m.put(rand.nextInt(size), 2);
                    } else {
                        m.remove(rand.nextInt(size));
                    }
                    m.get(rand.nextInt(size));
                } catch (ConcurrentModificationException e) {
                    detected.incrementAndGet();
                } catch (NegativeArraySizeException e) {
                    notDetected.incrementAndGet();
                } catch (ArrayIndexOutOfBoundsException e) {
                    notDetected.incrementAndGet();
                } catch (IllegalArgumentException e) {
                    notDetected.incrementAndGet();
                } catch (NullPointerException e) {
                    notDetected.incrementAndGet();
                }
            }
            s.commit();
            Thread.sleep(1);
        }
    } finally {
        task.get();
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) ConcurrentModificationException(java.util.ConcurrentModificationException) Random(java.util.Random)

Example 68 with Task

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

the class TestMvccMultiThreaded method testConcurrentMerge.

private void testConcurrentMerge() throws Exception {
    deleteDb(getTestName());
    int len = 3;
    final Connection[] connList = new Connection[len];
    for (int i = 0; i < len; i++) {
        Connection conn = getConnection(getTestName() + ";MVCC=TRUE;LOCK_TIMEOUT=500");
        connList[i] = conn;
    }
    Connection conn = connList[0];
    conn.createStatement().execute("create table test(id int primary key, name varchar)");
    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() {

            @Override
            public void call() throws Exception {
                while (!stop) {
                    c.createStatement().execute("merge into test values(1, 'x')");
                    c.commit();
                    Thread.sleep(1);
                }
            }
        };
        tasks[i].execute();
    }
    Thread.sleep(1000);
    stop[0] = true;
    for (int i = 0; i < len; i++) {
        tasks[i].get();
    }
    for (int i = 0; i < len; i++) {
        connList[i].close();
    }
    deleteDb(getTestName());
}
Also used : Task(org.h2.util.Task) Connection(java.sql.Connection) SQLException(java.sql.SQLException)

Example 69 with Task

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

the class TestMvccMultiThreaded method testConcurrentUpdate.

private void testConcurrentUpdate() throws Exception {
    deleteDb(getTestName());
    int len = 2;
    final Connection[] connList = new Connection[len];
    for (int i = 0; i < len; i++) {
        connList[i] = getConnection(getTestName() + ";MVCC=TRUE");
    }
    Connection conn = connList[0];
    conn.createStatement().execute("create table test(id int primary key, value int)");
    conn.createStatement().execute("insert into test values(0, 0)");
    final int count = 1000;
    Task[] tasks = new Task[len];
    final CountDownLatch latch = new CountDownLatch(len);
    for (int i = 0; i < len; i++) {
        final int x = i;
        tasks[i] = new Task() {

            @Override
            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();
                }
            }
        };
        tasks[i].execute();
    }
    for (int i = 0; i < len; i++) {
        tasks[i].get();
    }
    ResultSet rs = conn.createStatement().executeQuery("select value from test");
    rs.next();
    assertEquals(count * len, rs.getInt(1));
    for (int i = 0; i < len; i++) {
        connList[i].close();
    }
}
Also used : Task(org.h2.util.Task) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) CountDownLatch(java.util.concurrent.CountDownLatch) SQLException(java.sql.SQLException)

Example 70 with Task

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

the class TestRowLocks method testCases.

private void testCases() throws Exception {
    deleteDb(getTestName());
    c1 = getConnection(getTestName() + ";MVCC=TRUE");
    s1 = c1.createStatement();
    s1.execute("SET LOCK_TIMEOUT 10000");
    s1.execute("CREATE TABLE TEST AS " + "SELECT X ID, 'Hello' NAME FROM SYSTEM_RANGE(1, 3)");
    c1.commit();
    c1.setAutoCommit(false);
    s1.execute("UPDATE TEST SET NAME='Hallo' WHERE ID=1");
    c2 = getConnection(getTestName());
    c2.setAutoCommit(false);
    s2 = c2.createStatement();
    assertEquals("Hallo", getSingleValue(s1, "SELECT NAME FROM TEST WHERE ID=1"));
    assertEquals("Hello", getSingleValue(s2, "SELECT NAME FROM TEST WHERE ID=1"));
    s2.execute("UPDATE TEST SET NAME='Hallo' WHERE ID=2");
    assertThrows(ErrorCode.LOCK_TIMEOUT_1, s2).executeUpdate("UPDATE TEST SET NAME='Hi' WHERE ID=1");
    c1.commit();
    c2.commit();
    assertEquals("Hallo", getSingleValue(s1, "SELECT NAME FROM TEST WHERE ID=1"));
    assertEquals("Hallo", getSingleValue(s2, "SELECT NAME FROM TEST WHERE ID=1"));
    s2.execute("UPDATE TEST SET NAME='H1' WHERE ID=1");
    Task task = new Task() {

        @Override
        public void call() throws SQLException {
            s1.execute("UPDATE TEST SET NAME='H2' WHERE ID=1");
        }
    };
    task.execute();
    Thread.sleep(100);
    c2.commit();
    task.get();
    c1.commit();
    assertEquals("H2", getSingleValue(s1, "SELECT NAME FROM TEST WHERE ID=1"));
    assertEquals("H2", getSingleValue(s2, "SELECT NAME FROM TEST WHERE ID=1"));
    c1.close();
    c2.close();
}
Also used : Task(org.h2.util.Task)

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