Search in sources :

Example 36 with Task

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

the class TestDeadlock method testConcurrentLobReadAndTempResultTableDelete.

private void testConcurrentLobReadAndTempResultTableDelete() throws Exception {
    deleteDb("deadlock");
    String url = "deadlock;MAX_MEMORY_ROWS=10";
    Connection conn, conn2;
    Statement stat2;
    conn = getConnection(url);
    conn2 = getConnection(url);
    final Statement stat = conn.createStatement();
    stat2 = conn2.createStatement();
    stat.execute("create table test(id int primary key, name varchar) as " + "select x, 'Hello' from system_range(1,20)");
    stat2.execute("create table test_clob(id int primary key, data clob) as " + "select 1, space(10000)");
    ResultSet rs2 = stat2.executeQuery("select * from test_clob");
    rs2.next();
    Task t = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                stat.execute("select * from (select distinct id from test)");
            }
        }
    };
    t.execute();
    long start = System.nanoTime();
    while (System.nanoTime() - start < TimeUnit.SECONDS.toNanos(1)) {
        Reader r = rs2.getCharacterStream(2);
        char[] buff = new char[1024];
        while (true) {
            int x = r.read(buff);
            if (x < 0) {
                break;
            }
        }
    }
    t.get();
    stat.execute("drop all objects");
    conn.close();
    conn2.close();
}
Also used : Task(org.h2.util.Task) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Reader(java.io.Reader)

Example 37 with Task

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

the class TestExclusive method test.

@Override
public void test() throws Exception {
    deleteDb("exclusive");
    Connection conn = getConnection("exclusive");
    Statement stat = conn.createStatement();
    stat.execute("set exclusive true");
    assertThrows(ErrorCode.DATABASE_IS_IN_EXCLUSIVE_MODE, this).getConnection("exclusive");
    stat.execute("set exclusive false");
    Connection conn2 = getConnection("exclusive");
    final Statement stat2 = conn2.createStatement();
    stat.execute("set exclusive true");
    final AtomicInteger state = new AtomicInteger(0);
    Task task = new Task() {

        @Override
        public void call() throws SQLException {
            stat2.execute("select * from dual");
            if (state.get() != 1) {
                new Error("unexpected state: " + state.get()).printStackTrace();
            }
        }
    };
    task.execute();
    state.set(1);
    stat.execute("set exclusive false");
    task.get();
    stat.execute("set exclusive true");
    conn.close();
    // check that exclusive mode is off when disconnected
    stat2.execute("select * from dual");
    conn2.close();
    deleteDb("exclusive");
}
Also used : Task(org.h2.util.Task) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 38 with Task

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

the class TestSequence method testConcurrentCreate.

private void testConcurrentCreate() throws Exception {
    deleteDb("sequence");
    final String url = getURL("sequence;MULTI_THREADED=1;LOCK_TIMEOUT=2000", true);
    Connection conn = getConnection(url);
    Task[] tasks = new Task[2];
    try {
        Statement stat = conn.createStatement();
        stat.execute("create table dummy(id bigint primary key)");
        stat.execute("create table test(id bigint primary key)");
        stat.execute("create sequence test_seq cache 2");
        for (int i = 0; i < tasks.length; i++) {
            final int x = i;
            tasks[i] = new Task() {

                @Override
                public void call() throws Exception {
                    try (Connection conn = getConnection(url)) {
                        PreparedStatement prep = conn.prepareStatement("insert into test(id) values(next value for test_seq)");
                        PreparedStatement prep2 = conn.prepareStatement("delete from test");
                        while (!stop) {
                            prep.execute();
                            if (Math.random() < 0.01) {
                                prep2.execute();
                            }
                            if (Math.random() < 0.01) {
                                createDropTrigger(conn);
                            }
                        }
                    }
                }

                private void createDropTrigger(Connection conn) throws Exception {
                    String triggerName = "t_" + x;
                    Statement stat = conn.createStatement();
                    stat.execute("create trigger " + triggerName + " before insert on dummy call \"" + TriggerTest.class.getName() + "\"");
                    stat.execute("drop trigger " + triggerName);
                }
            }.execute();
        }
        Thread.sleep(1000);
        for (Task t : tasks) {
            t.get();
        }
    } finally {
        for (Task t : tasks) {
            t.join();
        }
        conn.close();
    }
}
Also used : Task(org.h2.util.Task) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 39 with Task

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

the class TestMultiThread method testConcurrentLobAdd.

private void testConcurrentLobAdd() throws Exception {
    String db = getTestName();
    deleteDb(db);
    final String url = getURL(db + ";MULTI_THREADED=1", true);
    try (Connection conn = getConnection(url)) {
        Statement stat = conn.createStatement();
        stat.execute("create table test(id identity, data clob)");
        Task[] tasks = new Task[2];
        for (int i = 0; i < tasks.length; i++) {
            Task t = new Task() {

                @Override
                public void call() throws Exception {
                    try (Connection c2 = getConnection(url)) {
                        PreparedStatement p2 = c2.prepareStatement("insert into test(data) values(?)");
                        while (!stop) {
                            p2.setCharacterStream(1, new StringReader(new String(new char[10 * 1024])));
                            p2.execute();
                        }
                    }
                }
            };
            tasks[i] = t;
            t.execute();
        }
        Thread.sleep(500);
        for (Task t : tasks) {
            t.get();
        }
    }
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) StringReader(java.io.StringReader) PreparedStatement(java.sql.PreparedStatement)

Example 40 with Task

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

the class TestMultiThread method testConcurrentAlter.

private void testConcurrentAlter() throws Exception {
    deleteDb(getTestName());
    try (final Connection conn = getConnection(getTestName())) {
        Statement stat = conn.createStatement();
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                while (!stop) {
                    conn.prepareStatement("select * from test");
                }
            }
        };
        stat.execute("create table test(id int)");
        t.execute();
        for (int i = 0; i < 200; i++) {
            stat.execute("alter table test add column x int");
            stat.execute("alter table test drop column x");
        }
        t.get();
    }
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

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