Search in sources :

Example 71 with Task

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

the class TestCacheConcurrentLIRS method testConcurrent.

private void testConcurrent() {
    CacheLongKeyLIRS.Config cc = new CacheLongKeyLIRS.Config();
    cc.maxMemory = 100;
    final CacheLongKeyLIRS<Integer> test = new CacheLongKeyLIRS<>(cc);
    int threadCount = 8;
    final CountDownLatch wait = new CountDownLatch(1);
    final AtomicBoolean stopped = new AtomicBoolean();
    Task[] tasks = new Task[threadCount];
    final int[] getCounts = new int[threadCount];
    final int offset = 1000000;
    for (int i = 0; i < 100; i++) {
        test.put(offset + i, i);
    }
    final int[] keys = new int[1000];
    Random random = new Random(1);
    for (int i = 0; i < keys.length; i++) {
        int key;
        do {
            key = (int) Math.abs(random.nextGaussian() * 50);
        } while (key > 100);
        keys[i] = key;
    }
    for (int i = 0; i < threadCount; i++) {
        final int x = i;
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                Random random = new Random(x);
                wait.await();
                int i = 0;
                for (; !stopped.get(); i++) {
                    int key = keys[random.nextInt(keys.length)];
                    test.get(offset + key);
                    if ((i & 127) == 0) {
                        test.put(offset + random.nextInt(100), random.nextInt());
                    }
                }
                getCounts[x] = i;
            }
        };
        t.execute("t" + i);
        tasks[i] = t;
    }
    wait.countDown();
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    stopped.set(true);
    for (Task t : tasks) {
        t.get();
    }
    int totalCount = 0;
    for (int x : getCounts) {
        totalCount += x;
    }
    trace("requests: " + totalCount);
}
Also used : CacheLongKeyLIRS(org.h2.mvstore.cache.CacheLongKeyLIRS) Task(org.h2.util.Task) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random)

Example 72 with Task

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

the class TestTriggersConstraints method testTriggerDeadlock.

private void testTriggerDeadlock() throws Exception {
    final Connection conn, conn2;
    final Statement stat, stat2;
    conn = getConnection("trigger");
    conn2 = getConnection("trigger");
    stat = conn.createStatement();
    stat2 = conn2.createStatement();
    stat.execute("create table test(id int) as select 1");
    stat.execute("create table test2(id int) as select 1");
    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() {

        @Override
        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();
    stat.execute("drop table test");
    stat.execute("drop table test2");
    conn.close();
    conn2.close();
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

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