use of org.h2.command.dml.Set in project h2database by h2database.
the class TestMVTableEngine method testLocking.
private void testLocking() throws Exception {
deleteDb(getTestName());
String dbName = getTestName() + ";MV_STORE=TRUE;MVCC=FALSE";
Connection conn = getConnection(dbName);
Statement stat = conn.createStatement();
stat.execute("set lock_timeout 1000");
stat.execute("create table a(id int primary key, name varchar)");
stat.execute("create table b(id int primary key, name varchar)");
Connection conn1 = getConnection(dbName);
final Statement stat1 = conn1.createStatement();
stat1.execute("set lock_timeout 1000");
conn.setAutoCommit(false);
conn1.setAutoCommit(false);
stat.execute("insert into a values(1, 'Hello')");
stat1.execute("insert into b values(1, 'Hello')");
Task t = new Task() {
@Override
public void call() throws Exception {
stat1.execute("insert into a values(2, 'World')");
}
};
t.execute();
try {
stat.execute("insert into b values(2, 'World')");
throw t.getException();
} catch (SQLException e) {
assertEquals(e.toString(), ErrorCode.DEADLOCK_1, e.getErrorCode());
}
conn1.close();
conn.close();
}
use of org.h2.command.dml.Set in project h2database by h2database.
the class TestConcurrentUpdate method test.
@Override
public void test() throws Exception {
deleteDb("concurrent");
final String url = getURL("concurrent;MULTI_THREADED=TRUE", true);
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar)");
Task[] tasks = new Task[THREADS];
for (int i = 0; i < THREADS; i++) {
final int threadId = i;
Task t = new Task() {
@Override
public void call() throws Exception {
Random r = new Random(threadId);
Connection conn = getConnection(url);
PreparedStatement insert = conn.prepareStatement("insert into test values(?, ?)");
PreparedStatement update = conn.prepareStatement("update test set name = ? where id = ?");
PreparedStatement delete = conn.prepareStatement("delete from test where id = ?");
PreparedStatement select = conn.prepareStatement("select * from test where id = ?");
while (!stop) {
try {
int x = r.nextInt(ROW_COUNT);
String data = "x" + r.nextInt(ROW_COUNT);
switch(r.nextInt(3)) {
case 0:
insert.setInt(1, x);
insert.setString(2, data);
insert.execute();
break;
case 1:
update.setString(1, data);
update.setInt(2, x);
update.execute();
break;
case 2:
delete.setInt(1, x);
delete.execute();
break;
case 4:
select.setInt(1, x);
ResultSet rs = select.executeQuery();
while (rs.next()) {
rs.getString(2);
}
break;
}
} catch (SQLException e) {
handleException(e);
}
}
conn.close();
}
};
tasks[i] = t;
t.execute();
}
// test 2 seconds
for (int i = 0; i < 200; i++) {
Thread.sleep(10);
for (Task t : tasks) {
if (t.isFinished()) {
i = 1000;
break;
}
}
}
for (Task t : tasks) {
t.get();
}
conn.close();
}
use of org.h2.command.dml.Set in project h2database by h2database.
the class TestCrashAPI method getConnection.
private Connection getConnection(int seed, boolean delete) throws SQLException {
openCount++;
if (delete) {
deleteDb();
}
// can not use FILE_LOCK=NO, otherwise something could be written into
// the database in the finalize method
String add = ";MAX_QUERY_TIMEOUT=10000";
// int testing;
// if(openCount >= 32) {
// int test;
// Runtime.getRuntime().halt(0);
// System.exit(1);
// }
// System.out.println("now open " + openCount);
// add += ";TRACE_LEVEL_FILE=3";
// config.logMode = 2;
// }
String dbName = "crashApi" + seed;
String url = getURL(DIR + "/" + dbName, true) + add;
// int test;
// url += ";DB_CLOSE_ON_EXIT=FALSE";
// int test;
// url += ";TRACE_LEVEL_FILE=3";
Connection conn = null;
String fileName = "temp/backup/db-" + uniqueId++ + ".zip";
Backup.execute(fileName, getBaseDir() + "/" + DIR, dbName, true);
// close databases earlier
System.gc();
try {
conn = DriverManager.getConnection(url, "sa", getPassword(""));
// delete the backup if opening was successful
FileUtils.delete(fileName);
} catch (SQLException e) {
if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) {
// delete if the password changed
FileUtils.delete(fileName);
}
throw e;
}
int len = random.getInt(50);
int first = random.getInt(statements.size() - len);
int end = first + len;
Statement stat = conn.createStatement();
stat.execute("SET LOCK_TIMEOUT 10");
stat.execute("SET WRITE_DELAY 0");
if (random.nextBoolean()) {
if (random.nextBoolean()) {
double g = random.nextGaussian();
int size = (int) Math.abs(10000 * g * g);
stat.execute("SET CACHE_SIZE " + size);
} else {
stat.execute("SET CACHE_SIZE 0");
}
}
stat.execute("SCRIPT NOPASSWORDS NOSETTINGS");
for (int i = first; i < end && i < statements.size() && !stopped; i++) {
try {
stat.execute("SELECT * FROM TEST WHERE ID=1");
} catch (Throwable t) {
printIfBad(seed, -i, -1, t);
}
try {
stat.execute("SELECT * FROM TEST WHERE ID=1 OR ID=1");
} catch (Throwable t) {
printIfBad(seed, -i, -1, t);
}
String sql = statements.get(i);
try {
// if(openCount == 32) {
// int test;
// System.out.println("stop!");
// }
stat.execute(sql);
} catch (Throwable t) {
printIfBad(seed, -i, -1, t);
}
}
if (random.nextBoolean()) {
try {
conn.commit();
} catch (Throwable t) {
printIfBad(seed, 0, -1, t);
}
}
return conn;
}
use of org.h2.command.dml.Set in project h2database by h2database.
the class TestCrashAPI method testCase.
private void testCase(int seed) throws SQLException {
printTime("seed: " + seed);
callCount = 0;
openCount = 0;
random = new RandomGen();
random.setSeed(seed);
Connection c1 = getConnection(seed, true);
Connection conn = null;
for (int i = 0; i < 2000 && !stopped; i++) {
if (objects.size() == 0) {
try {
conn = getConnection(seed, false);
} catch (SQLException e) {
if ("08004".equals(e.getSQLState())) {
// Wrong user/password [08004]
try {
c1.createStatement().execute("SET PASSWORD ''");
} catch (Throwable t) {
// power off or so
break;
}
try {
conn = getConnection(seed, false);
} catch (Throwable t) {
printIfBad(seed, -i, -1, t);
}
} else if ("90098".equals(e.getSQLState())) {
// The database has been closed
break;
} else {
printIfBad(seed, -i, -1, e);
}
}
objects.add(conn);
}
int objectId = random.getInt(objects.size());
if (random.getBoolean(1)) {
objects.remove(objectId);
continue;
}
if (random.getInt(2000) == 0 && conn != null) {
((JdbcConnection) conn).setPowerOffCount(random.getInt(50));
}
Object o = objects.get(objectId);
if (o == null) {
objects.remove(objectId);
continue;
}
Class<?> in = getJdbcInterface(o);
ArrayList<Method> methods = classMethods.get(in);
Method m = methods.get(random.getInt(methods.size()));
Object o2 = callRandom(seed, i, objectId, o, m);
if (o2 != null) {
objects.add(o2);
}
}
try {
if (conn != null) {
conn.close();
}
c1.close();
} catch (Throwable t) {
printIfBad(seed, -101010, -1, t);
try {
deleteDb();
} catch (Throwable t2) {
printIfBad(seed, -101010, -1, t2);
}
}
objects.clear();
}
use of org.h2.command.dml.Set in project h2database by h2database.
the class TestFuzzOptimizations method testInSelect.
private void testInSelect() {
Db db = new Db(conn);
db.execute("CREATE TABLE TEST(A INT, B INT)");
db.execute("CREATE INDEX IDX ON TEST(A)");
db.execute("INSERT INTO TEST SELECT X/4, MOD(X, 4) " + "FROM SYSTEM_RANGE(1, 16)");
db.execute("UPDATE TEST SET A = NULL WHERE A = 0");
db.execute("UPDATE TEST SET B = NULL WHERE B = 0");
Random random = new Random();
long seed = random.nextLong();
println("seed: " + seed);
for (int i = 0; i < 100; i++) {
String column = random.nextBoolean() ? "A" : "B";
String value = new String[] { "NULL", "0", "A", "B" }[random.nextInt(4)];
String compare = random.nextBoolean() ? "A" : "B";
int x = random.nextInt(3);
String sql1 = "SELECT * FROM TEST T WHERE " + column + "+0 " + "IN(SELECT " + value + " FROM TEST I WHERE I." + compare + "=?) ORDER BY 1, 2";
String sql2 = "SELECT * FROM TEST T WHERE " + column + " " + "IN(SELECT " + value + " FROM TEST I WHERE I." + compare + "=?) ORDER BY 1, 2";
List<Map<String, Object>> a = db.prepare(sql1).set(x).query();
List<Map<String, Object>> b = db.prepare(sql2).set(x).query();
assertTrue("seed: " + seed + " sql: " + sql1 + " a: " + a + " b: " + b, a.equals(b));
}
db.execute("DROP TABLE TEST");
}
Aggregations