Search in sources :

Example 81 with Call

use of org.h2.command.dml.Call 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)

Example 82 with Call

use of org.h2.command.dml.Call 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 83 with Call

use of org.h2.command.dml.Call 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 84 with Call

use of org.h2.command.dml.Call 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 85 with Call

use of org.h2.command.dml.Call 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)

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