Search in sources :

Example 41 with Call

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

the class TestMVTableEngine method testLocking.

private void testLocking() throws Exception {
    deleteDb(getTestName());
    String dbName = getTestName() + ";MV_STORE=TRUE;MVCC=FALSE";
    Connection conn = getConnection(dbName);
    Statement stat = conn.createStatement();
    stat.execute("set lock_timeout 1000");
    stat.execute("create table a(id int primary key, name varchar)");
    stat.execute("create table b(id int primary key, name varchar)");
    Connection conn1 = getConnection(dbName);
    final Statement stat1 = conn1.createStatement();
    stat1.execute("set lock_timeout 1000");
    conn.setAutoCommit(false);
    conn1.setAutoCommit(false);
    stat.execute("insert into a values(1, 'Hello')");
    stat1.execute("insert into b values(1, 'Hello')");
    Task t = new Task() {

        @Override
        public void call() throws Exception {
            stat1.execute("insert into a values(2, 'World')");
        }
    };
    t.execute();
    try {
        stat.execute("insert into b values(2, 'World')");
        throw t.getException();
    } catch (SQLException e) {
        assertEquals(e.toString(), ErrorCode.DEADLOCK_1, e.getErrorCode());
    }
    conn1.close();
    conn.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)

Example 42 with Call

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

the class TestTransactionStore method testStopWhileCommitting.

private void testStopWhileCommitting() throws Exception {
    String fileName = getBaseDir() + "/testStopWhileCommitting.h3";
    FileUtils.delete(fileName);
    Random r = new Random(0);
    for (int i = 0; i < 10; ) {
        MVStore s;
        TransactionStore ts;
        Transaction tx;
        TransactionMap<Integer, String> m;
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        ts.init();
        tx = ts.begin();
        s.setReuseSpace(false);
        m = tx.openMap("test");
        final String value = "x" + i;
        for (int j = 0; j < 1000; j++) {
            m.put(j, value);
        }
        final AtomicInteger state = new AtomicInteger();
        final MVStore store = s;
        final MVMap<Integer, String> other = s.openMap("other");
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                for (int i = 0; !stop; i++) {
                    state.set(i);
                    other.put(i, value);
                    store.commit();
                }
            }
        };
        task.execute();
        // wait for the task to start
        while (state.get() < 1) {
            Thread.yield();
        }
        // commit while writing in the task
        tx.commit();
        // wait for the task to stop
        task.get();
        store.close();
        s = MVStore.open(fileName);
        // roll back a bit, until we have some undo log entries
        assertTrue(s.hasMap("undoLog"));
        for (int back = 0; back < 100; back++) {
            int minus = r.nextInt(10);
            s.rollbackTo(Math.max(0, s.getCurrentVersion() - minus));
            MVMap<?, ?> undo = s.openMap("undoLog");
            if (undo.size() > 0) {
                break;
            }
        }
        // re-open the store, because we have opened
        // the undoLog map with the wrong data type
        s.close();
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        List<Transaction> list = ts.getOpenTransactions();
        if (list.size() != 0) {
            tx = list.get(0);
            if (tx.getStatus() == Transaction.STATUS_COMMITTING) {
                i++;
            }
        }
        s.close();
        FileUtils.delete(fileName);
        assertFalse(FileUtils.exists(fileName));
    }
}
Also used : Task(org.h2.util.Task) MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Random(java.util.Random) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 43 with Call

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

the class TestConcurrentUpdate method test.

@Override
public void test() throws Exception {
    deleteDb("concurrent");
    final String url = getURL("concurrent;MULTI_THREADED=TRUE", true);
    Connection conn = getConnection(url);
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar)");
    Task[] tasks = new Task[THREADS];
    for (int i = 0; i < THREADS; i++) {
        final int threadId = i;
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                Random r = new Random(threadId);
                Connection conn = getConnection(url);
                PreparedStatement insert = conn.prepareStatement("insert into test values(?, ?)");
                PreparedStatement update = conn.prepareStatement("update test set name = ? where id = ?");
                PreparedStatement delete = conn.prepareStatement("delete from test where id = ?");
                PreparedStatement select = conn.prepareStatement("select * from test where id = ?");
                while (!stop) {
                    try {
                        int x = r.nextInt(ROW_COUNT);
                        String data = "x" + r.nextInt(ROW_COUNT);
                        switch(r.nextInt(3)) {
                            case 0:
                                insert.setInt(1, x);
                                insert.setString(2, data);
                                insert.execute();
                                break;
                            case 1:
                                update.setString(1, data);
                                update.setInt(2, x);
                                update.execute();
                                break;
                            case 2:
                                delete.setInt(1, x);
                                delete.execute();
                                break;
                            case 4:
                                select.setInt(1, x);
                                ResultSet rs = select.executeQuery();
                                while (rs.next()) {
                                    rs.getString(2);
                                }
                                break;
                        }
                    } catch (SQLException e) {
                        handleException(e);
                    }
                }
                conn.close();
            }
        };
        tasks[i] = t;
        t.execute();
    }
    // test 2 seconds
    for (int i = 0; i < 200; i++) {
        Thread.sleep(10);
        for (Task t : tasks) {
            if (t.isFinished()) {
                i = 1000;
                break;
            }
        }
    }
    for (Task t : tasks) {
        t.get();
    }
    conn.close();
}
Also used : Task(org.h2.util.Task) Random(java.util.Random) SQLException(java.sql.SQLException) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 44 with Call

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

the class TestConcurrentLinkedList method testPerformance.

private static void testPerformance(final boolean stock) {
    System.out.print(stock ? "stock " : "custom ");
    long start = System.nanoTime();
    // final ConcurrentLinkedList<Integer> test =
    // new ConcurrentLinkedList<Integer>();
    final ConcurrentArrayList<Integer> test = new ConcurrentArrayList<>();
    final LinkedList<Integer> x = new LinkedList<>();
    final AtomicInteger counter = new AtomicInteger();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                if (stock) {
                    synchronized (x) {
                        Integer y = x.peekFirst();
                        if (y == null) {
                            counter.incrementAndGet();
                        }
                    }
                } else {
                    Integer y = test.peekFirst();
                    if (y == null) {
                        counter.incrementAndGet();
                    }
                }
            }
        }
    };
    task.execute();
    test.add(-1);
    x.add(-1);
    for (int i = 0; i < 2000000; i++) {
        Integer value = Integer.valueOf(i & 63);
        if (stock) {
            synchronized (x) {
                Integer f = x.peekLast();
                if (f != value) {
                    x.add(i);
                }
            }
            Math.sin(i);
            synchronized (x) {
                if (x.peekFirst() != x.peekLast()) {
                    x.removeFirst();
                }
            }
        } else {
            Integer f = test.peekLast();
            if (f != value) {
                test.add(i);
            }
            Math.sin(i);
            f = test.peekFirst();
            if (f != test.peekLast()) {
                test.removeFirst(f);
            }
        }
    }
    task.get();
    System.out.println(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) ConcurrentArrayList(org.h2.mvstore.ConcurrentArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LinkedList(java.util.LinkedList)

Example 45 with Call

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

the class TestConcurrentLinkedList method testConcurrent.

private void testConcurrent() {
    final ConcurrentArrayList<Integer> test = new ConcurrentArrayList<>();
    // final ConcurrentRing<Integer> test = new ConcurrentRing<Integer>();
    final AtomicInteger counter = new AtomicInteger();
    final AtomicInteger size = new AtomicInteger();
    Task task = new Task() {

        @Override
        public void call() {
            while (!stop) {
                Thread.yield();
                if (size.get() < 10) {
                    test.add(counter.getAndIncrement());
                    size.getAndIncrement();
                }
            }
        }
    };
    task.execute();
    for (int i = 0; i < 1000000; ) {
        Thread.yield();
        Integer x = test.peekFirst();
        if (x == null) {
            continue;
        }
        assertEquals(i, x.intValue());
        if (test.removeFirst(x)) {
            size.getAndDecrement();
            i++;
        }
    }
    task.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) ConcurrentArrayList(org.h2.mvstore.ConcurrentArrayList) 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