use of org.h2.command.dml.Call 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();
}
}
use of org.h2.command.dml.Call in project h2database by h2database.
the class TestMultiThread method testConcurrentInsert.
private void testConcurrentInsert() throws Exception {
deleteDb("lockMode");
final String url = getURL("lockMode;MULTI_THREADED=1;LOCK_TIMEOUT=10000", true);
int threadCount = 25;
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
Connection conn = getConnection(url);
try {
conn.createStatement().execute("CREATE TABLE IF NOT EXISTS TRAN (ID NUMBER(18,0) not null PRIMARY KEY)");
final ArrayList<Callable<Void>> callables = new ArrayList<>();
for (int i = 0; i < threadCount; i++) {
final long initialTransactionId = i * 1000000L;
callables.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
try (Connection taskConn = getConnection(url)) {
taskConn.setAutoCommit(false);
PreparedStatement insertTranStmt = taskConn.prepareStatement("INSERT INTO tran (id) VALUES(?)");
// to guarantee uniqueness
long tranId = initialTransactionId;
for (int j = 0; j < 1000; j++) {
insertTranStmt.setLong(1, tranId++);
insertTranStmt.execute();
taskConn.commit();
}
}
return null;
}
});
}
final ArrayList<Future<Void>> jobs = new ArrayList<>();
for (int i = 0; i < threadCount; i++) {
jobs.add(executor.submit(callables.get(i)));
}
// check for exceptions
for (Future<Void> job : jobs) {
job.get(5, TimeUnit.MINUTES);
}
} finally {
IOUtils.closeSilently(conn);
executor.shutdown();
executor.awaitTermination(20, TimeUnit.SECONDS);
}
deleteDb("lockMode");
}
use of org.h2.command.dml.Call in project h2database by h2database.
the class TestMultiThread method testConcurrentUpdate.
private void testConcurrentUpdate() throws Exception {
deleteDb("lockMode");
final int objectCount = 10000;
final String url = getURL("lockMode;MULTI_THREADED=1;LOCK_TIMEOUT=10000", true);
int threadCount = 25;
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
Connection conn = getConnection(url);
try {
conn.createStatement().execute("CREATE TABLE IF NOT EXISTS ACCOUNT" + "(ID NUMBER(18,0) not null PRIMARY KEY, BALANCE NUMBER null)");
final PreparedStatement mergeAcctStmt = conn.prepareStatement("MERGE INTO Account(id, balance) key (id) VALUES (?, ?)");
for (int i = 0; i < objectCount; i++) {
mergeAcctStmt.setLong(1, i);
mergeAcctStmt.setBigDecimal(2, BigDecimal.ZERO);
mergeAcctStmt.execute();
}
final ArrayList<Callable<Void>> callables = new ArrayList<>();
for (int i = 0; i < threadCount; i++) {
callables.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
try (Connection taskConn = getConnection(url)) {
taskConn.setAutoCommit(false);
final PreparedStatement updateAcctStmt = taskConn.prepareStatement("UPDATE account SET balance = ? WHERE id = ?");
for (int j = 0; j < 1000; j++) {
updateAcctStmt.setDouble(1, Math.random());
updateAcctStmt.setLong(2, (int) (Math.random() * objectCount));
updateAcctStmt.execute();
taskConn.commit();
}
}
return null;
}
});
}
final ArrayList<Future<Void>> jobs = new ArrayList<>();
for (int i = 0; i < threadCount; i++) {
jobs.add(executor.submit(callables.get(i)));
}
// check for exceptions
for (Future<Void> job : jobs) {
job.get(5, TimeUnit.MINUTES);
}
} finally {
IOUtils.closeSilently(conn);
executor.shutdown();
executor.awaitTermination(20, TimeUnit.SECONDS);
}
deleteDb("lockMode");
}
use of org.h2.command.dml.Call in project h2database by h2database.
the class TestMultiThread method testConcurrentView.
private void testConcurrentView() throws Exception {
if (config.mvcc || config.mvStore) {
return;
}
String db = getTestName();
deleteDb(db);
final String url = getURL(db + ";MULTI_THREADED=1", true);
final Random r = new Random();
try (Connection conn = getConnection(url)) {
Statement stat = conn.createStatement();
StringBuilder buff = new StringBuilder();
buff.append("create table test(id int");
final int len = 3;
for (int i = 0; i < len; i++) {
buff.append(", x" + i + " int");
}
buff.append(")");
stat.execute(buff.toString());
stat.execute("create view test_view as select * from test");
stat.execute("insert into test(id) select x from system_range(1, 2)");
Task t = new Task() {
@Override
public void call() throws Exception {
Connection c2 = getConnection(url);
while (!stop) {
c2.prepareStatement("select * from test_view where x" + r.nextInt(len) + "=1");
}
c2.close();
}
};
t.execute();
SynchronizedVerifier.setDetect(SmallLRUCache.class, true);
for (int i = 0; i < 1000; i++) {
conn.prepareStatement("select * from test_view where x" + r.nextInt(len) + "=1");
}
t.get();
SynchronizedVerifier.setDetect(SmallLRUCache.class, false);
}
}
use of org.h2.command.dml.Call in project h2database by h2database.
the class TestOpenClose method testCase.
private void testCase() throws Exception {
if (config.memory) {
return;
}
org.h2.Driver.load();
deleteDb("openClose");
final String url = getURL("openClose;FILE_LOCK=NO", true);
final String user = getUser(), password = getPassword();
Connection conn = DriverManager.getConnection(url, user, password);
conn.createStatement().execute("drop table employee if exists");
conn.createStatement().execute("create table employee(id int primary key, name varchar, salary int)");
conn.close();
// previously using getSize(200, 1000);
// but for Ubuntu, the default ulimit is 1024,
// which breaks the test
int len = getSize(10, 50);
Task[] tasks = new Task[len];
for (int i = 0; i < len; i++) {
tasks[i] = new Task() {
@Override
public void call() throws SQLException {
Connection c = DriverManager.getConnection(url, user, password);
PreparedStatement prep = c.prepareStatement("insert into employee values(?, ?, 0)");
int id = getNextId();
prep.setInt(1, id);
prep.setString(2, "employee " + id);
prep.execute();
c.close();
}
};
tasks[i].execute();
}
// }
for (int i = 0; i < len; i++) {
tasks[i].get();
}
conn = DriverManager.getConnection(url, user, password);
ResultSet rs = conn.createStatement().executeQuery("select count(*) from employee");
rs.next();
assertEquals(len, rs.getInt(1));
conn.close();
}
Aggregations