Search in sources :

Example 31 with Task

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

the class TestMvcc2 method testConcurrentUpdate.

private void testConcurrentUpdate() throws Exception {
    Connection conn = getConnection();
    final Connection conn2 = getConnection();
    Statement stat = conn.createStatement();
    final Statement stat2 = conn2.createStatement();
    stat2.execute("set lock_timeout 1000");
    stat.execute("create table test(id int primary key, name varchar)");
    stat.execute("insert into test values(0, 'Hello')");
    conn.setAutoCommit(false);
    Task t = new Task() {

        @Override
        public void call() throws SQLException {
            stat2.execute("update test set name = 'Hallo'");
        }
    };
    stat.execute("update test set name = 'Hi'");
    t.execute();
    Thread.sleep(500);
    conn.commit();
    t.get();
    ResultSet rs;
    rs = stat.executeQuery("select name from test");
    rs.next();
    assertEquals("Hallo", rs.getString(1));
    stat.execute("drop table test");
    conn2.close();
    conn.close();
}
Also used : Task(org.h2.util.Task) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 32 with Task

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

the class TestMvcc2 method testConcurrentInsert.

private void testConcurrentInsert() throws Exception {
    Connection conn = getConnection();
    final Connection conn2 = getConnection();
    Statement stat = conn.createStatement();
    final Statement stat2 = conn2.createStatement();
    stat2.execute("set lock_timeout 1000");
    stat.execute("create table test(id int primary key, name varchar)");
    conn.setAutoCommit(false);
    final AtomicBoolean committed = new AtomicBoolean(false);
    Task t = new Task() {

        @Override
        public void call() throws SQLException {
            try {
                // System.out.println("insert2 hallo");
                stat2.execute("insert into test values(0, 'Hallo')");
            // System.out.println("insert2 hallo done");
            } catch (SQLException e) {
                // System.out.println("insert2 hallo e " + e);
                if (!committed.get()) {
                    throw e;
                }
            }
        }
    };
    // System.out.println("insert hello");
    stat.execute("insert into test values(0, 'Hello')");
    t.execute();
    Thread.sleep(500);
    // System.out.println("insert hello commit");
    committed.set(true);
    conn.commit();
    t.get();
    ResultSet rs;
    rs = stat.executeQuery("select name from test");
    rs.next();
    assertEquals("Hello", rs.getString(1));
    stat.execute("drop table test");
    conn2.close();
    conn.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Task(org.h2.util.Task) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 33 with Task

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

the class TestLob method testDeadlock.

/**
 * Test for issue 315: Java Level Deadlock on Database & Session Objects
 */
private void testDeadlock() throws Exception {
    deleteDb("lob");
    Connection conn = getConnection("lob");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name clob)");
    stat.execute("insert into test select x, space(10000) from system_range(1, 3)");
    final Connection conn2 = getConnection("lob");
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            Statement stat = conn2.createStatement();
            stat.setFetchSize(1);
            for (int i = 0; !stop; i++) {
                ResultSet rs = stat.executeQuery("select * from test where id > -" + i);
                while (rs.next()) {
                // ignore
                }
            }
        }
    };
    task.execute();
    stat.execute("create table test2(id int primary key, name clob)");
    for (int i = 0; i < 100; i++) {
        stat.execute("delete from test2");
        stat.execute("insert into test2 values(1, space(10000 + " + i + "))");
    }
    task.get();
    conn.close();
    conn2.close();
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint)

Example 34 with Task

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

the class TestLob method testConcurrentCreate.

private void testConcurrentCreate() throws Exception {
    deleteDb("lob");
    final JdbcConnection conn1 = (JdbcConnection) getConnection("lob");
    final JdbcConnection conn2 = (JdbcConnection) getConnection("lob");
    conn1.setAutoCommit(false);
    conn2.setAutoCommit(false);
    final byte[] buffer = new byte[10000];
    Task task1 = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Blob b = conn1.createBlob();
                OutputStream out = b.setBinaryStream(1);
                out.write(buffer);
                out.close();
            }
        }
    };
    Task task2 = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Blob b = conn2.createBlob();
                OutputStream out = b.setBinaryStream(1);
                out.write(buffer);
                out.close();
            }
        }
    };
    task1.execute();
    task2.execute();
    Thread.sleep(1000);
    task1.get();
    task2.get();
    conn1.close();
    conn2.close();
}
Also used : Task(org.h2.util.Task) Blob(java.sql.Blob) OutputStream(java.io.OutputStream) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 35 with Task

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

the class TestDeadlock method testDeadlockInFulltextSearch.

private void testDeadlockInFulltextSearch() throws SQLException {
    deleteDb("deadlock");
    String url = "deadlock";
    Connection conn, conn2;
    conn = getConnection(url);
    conn2 = getConnection(url);
    final Statement stat = conn.createStatement();
    Statement stat2 = conn2.createStatement();
    stat.execute("create alias if not exists ft_init for " + "\"org.h2.fulltext.FullText.init\"");
    stat.execute("call ft_init()");
    stat.execute("create table test(id int primary key, name varchar)");
    stat.execute("call ft_create_index('PUBLIC', 'TEST', null)");
    Task t = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                stat.executeQuery("select * from test");
            }
        }
    };
    t.execute();
    long start = System.nanoTime();
    while (System.nanoTime() - start < TimeUnit.SECONDS.toNanos(1)) {
        stat2.execute("insert into test values(1, 'Hello')");
        stat2.execute("delete from test");
    }
    t.get();
    conn2.close();
    conn.close();
    conn = getConnection(url);
    conn.createStatement().execute("drop all objects");
    conn.close();
}
Also used : Task(org.h2.util.Task) 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