use of org.h2.jdbc.JdbcConnection in project h2database by h2database.
the class DbUpgrade method upgrade.
private static void upgrade(ConnectionInfo ci, Properties info) throws SQLException {
String name = ci.getName();
String data = name + Constants.SUFFIX_OLD_DATABASE_FILE;
String index = name + ".index.db";
String lobs = name + ".lobs.db";
String backupData = data + ".backup";
String backupIndex = index + ".backup";
String backupLobs = lobs + ".backup";
String script = null;
try {
if (scriptInTempDir) {
new File(Utils.getProperty("java.io.tmpdir", ".")).mkdirs();
script = File.createTempFile("h2dbmigration", "backup.sql").getAbsolutePath();
} else {
script = name + ".script.sql";
}
String oldUrl = "jdbc:h2v1_1:" + name + ";UNDO_LOG=0;LOG=0;LOCK_MODE=0";
String cipher = ci.getProperty("CIPHER", null);
if (cipher != null) {
oldUrl += ";CIPHER=" + cipher;
}
Connection conn = DriverManager.getConnection(oldUrl, info);
Statement stat = conn.createStatement();
String uuid = UUID.randomUUID().toString();
if (cipher != null) {
stat.execute("script to '" + script + "' cipher aes password '" + uuid + "' --hide--");
} else {
stat.execute("script to '" + script + "'");
}
conn.close();
FileUtils.move(data, backupData);
FileUtils.move(index, backupIndex);
if (FileUtils.exists(lobs)) {
FileUtils.move(lobs, backupLobs);
}
ci.removeProperty("IFEXISTS", false);
conn = new JdbcConnection(ci, true);
stat = conn.createStatement();
if (cipher != null) {
stat.execute("runscript from '" + script + "' cipher aes password '" + uuid + "' --hide--");
} else {
stat.execute("runscript from '" + script + "'");
}
stat.execute("analyze");
stat.execute("shutdown compact");
stat.close();
conn.close();
if (deleteOldDb) {
FileUtils.delete(backupData);
FileUtils.delete(backupIndex);
FileUtils.deleteRecursive(backupLobs, false);
}
} catch (Exception e) {
if (FileUtils.exists(backupData)) {
FileUtils.move(backupData, data);
}
if (FileUtils.exists(backupIndex)) {
FileUtils.move(backupIndex, index);
}
if (FileUtils.exists(backupLobs)) {
FileUtils.move(backupLobs, lobs);
}
FileUtils.delete(name + ".h2.db");
throw DbException.toSQLException(e);
} finally {
if (script != null) {
FileUtils.delete(script);
}
}
}
use of org.h2.jdbc.JdbcConnection in project h2database by h2database.
the class TestMVTableEngine method testReuseDiskSpace.
private void testReuseDiskSpace() throws Exception {
deleteDb(getTestName());
String dbName = getTestName() + ";MV_STORE=TRUE";
Connection conn;
Statement stat;
long maxSize = 0;
for (int i = 0; i < 20; i++) {
conn = getConnection(dbName);
Database db = (Database) ((JdbcConnection) conn).getSession().getDataHandler();
db.getMvStore().getStore().setRetentionTime(0);
stat = conn.createStatement();
stat.execute("create table test(id int primary key, data varchar)");
stat.execute("insert into test select x, space(1000) " + "from system_range(1, 1000)");
stat.execute("drop table test");
conn.close();
long size = FileUtils.size(getBaseDir() + "/" + getTestName() + Constants.SUFFIX_MV_FILE);
if (i < 10) {
maxSize = (int) (Math.max(size, maxSize) * 1.1);
} else if (size > maxSize) {
fail(i + " size: " + size + " max: " + maxSize);
}
}
}
use of org.h2.jdbc.JdbcConnection in project h2database by h2database.
the class TestPowerOff method testMemoryTables.
private void testMemoryTables() throws SQLException {
if (config.networked) {
return;
}
deleteDb(dir, DB_NAME);
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("CREATE MEMORY TABLE TEST" + "(ID INT PRIMARY KEY, NAME VARCHAR(255))");
stat.execute("INSERT INTO TEST VALUES(1, 'Hello')");
stat.execute("CHECKPOINT");
((JdbcConnection) conn).setPowerOffCount(1);
try {
stat.execute("INSERT INTO TEST VALUES(2, 'Hello')");
stat.execute("INSERT INTO TEST VALUES(3, 'Hello')");
stat.execute("CHECKPOINT");
fail();
} catch (SQLException e) {
assertKnownException(e);
}
((JdbcConnection) conn).setPowerOffCount(0);
try {
conn.close();
} catch (SQLException e) {
// ignore
}
conn = getConnection(url);
stat = conn.createStatement();
ResultSet rs = stat.executeQuery("SELECT COUNT(*) FROM TEST");
rs.next();
assertEquals(1, rs.getInt(1));
conn.close();
}
use of org.h2.jdbc.JdbcConnection in project h2database by h2database.
the class TestPowerOff method recoverAndCheckConsistency.
private int recoverAndCheckConsistency() throws SQLException {
int state;
Database.setInitialPowerOffCount(0);
Connection conn = getConnection(url);
assertEquals(0, ((JdbcConnection) conn).getPowerOffCount());
Statement stat = conn.createStatement();
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getTables(null, null, "TEST", null);
if (!rs.next()) {
state = 0;
} else {
// table does not exist
rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
if (!rs.next()) {
state = 1;
} else {
assertEquals(1, rs.getInt(1));
String name1 = rs.getString(2);
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
String name2 = rs.getString(2);
assertFalse(rs.next());
if ("Hello".equals(name1)) {
assertEquals("World", name2);
state = 2;
} else {
assertEquals("Hallo", name1);
assertEquals("Welt", name2);
state = 3;
}
}
}
conn.close();
return state;
}
use of org.h2.jdbc.JdbcConnection in project h2database by h2database.
the class TestPowerOff method testRun.
private int testRun(boolean init) throws SQLException {
if (init) {
Database.setInitialPowerOffCount(Integer.MAX_VALUE);
}
int state = 0;
Connection conn = null;
try {
conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("SET WRITE_DELAY 0");
stat.execute("CREATE TABLE IF NOT EXISTS TEST" + "(ID INT PRIMARY KEY, NAME VARCHAR(255))");
state = 1;
conn.setAutoCommit(false);
stat.execute("INSERT INTO TEST VALUES(1, 'Hello')");
stat.execute("INSERT INTO TEST VALUES(2, 'World')");
conn.commit();
state = 2;
stat.execute("UPDATE TEST SET NAME='Hallo' WHERE ID=1");
stat.execute("UPDATE TEST SET NAME='Welt' WHERE ID=2");
conn.commit();
state = 3;
stat.execute("DELETE FROM TEST WHERE ID=1");
stat.execute("DELETE FROM TEST WHERE ID=2");
conn.commit();
state = 1;
stat.execute("DROP TABLE TEST");
state = 0;
if (init) {
maxPowerOffCount = Integer.MAX_VALUE - ((JdbcConnection) conn).getPowerOffCount();
}
conn.close();
} catch (SQLException e) {
if (e.getSQLState().equals("" + ErrorCode.DATABASE_IS_CLOSED)) {
// this is ok
} else {
throw e;
}
}
JdbcUtils.closeSilently(conn);
return state;
}
Aggregations