Search in sources :

Example 26 with JdbcConnection

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);
        }
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) JdbcConnection(org.h2.jdbc.JdbcConnection) File(java.io.File) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 27 with JdbcConnection

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);
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) Database(org.h2.engine.Database) JdbcConnection(org.h2.jdbc.JdbcConnection) Savepoint(java.sql.Savepoint)

Example 28 with JdbcConnection

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();
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 29 with JdbcConnection

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;
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 30 with JdbcConnection

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;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Aggregations

JdbcConnection (org.h2.jdbc.JdbcConnection)27 SQLException (java.sql.SQLException)15 Connection (java.sql.Connection)14 Statement (java.sql.Statement)13 PreparedStatement (java.sql.PreparedStatement)9 Session (org.h2.engine.Session)8 ResultSet (java.sql.ResultSet)6 IOException (java.io.IOException)5 DbException (org.h2.message.DbException)4 Parser (org.h2.command.Parser)3 Task (org.h2.util.Task)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 PipedInputStream (java.io.PipedInputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 Savepoint (java.sql.Savepoint)2 Properties (java.util.Properties)2 IgniteH2Indexing (org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 Expression (org.h2.expression.Expression)2