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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations