Search in sources :

Example 6 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project h2database by h2database.

the class TestFileLockSerialized method testKillWriter.

private void testKillWriter() throws Exception {
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
    String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;WRITE_DELAY=0";
    Connection conn = getConnection(writeUrl, "sa", "sa");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key)");
    ((JdbcConnection) conn).setPowerOffCount(1);
    assertThrows(ErrorCode.DATABASE_IS_CLOSED, stat).execute("insert into test values(1)");
    Connection conn2 = getConnection(writeUrl, "sa", "sa");
    Statement stat2 = conn2.createStatement();
    stat2.execute("insert into test values(1)");
    printResult(stat2, "select * from test");
    conn2.close();
    assertThrows(ErrorCode.DATABASE_IS_CLOSED, conn).close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 7 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project h2database by h2database.

the class TestMVTableEngine method testReadOnly.

private void testReadOnly() throws Exception {
    if (config.memory) {
        return;
    }
    deleteDb(getTestName());
    String dbName = getTestName() + ";MV_STORE=TRUE";
    Connection conn;
    Statement stat;
    conn = getConnection(dbName);
    stat = conn.createStatement();
    stat.execute("create table test(id int)");
    conn.close();
    FileUtils.setReadOnly(getBaseDir() + "/" + getTestName() + Constants.SUFFIX_MV_FILE);
    conn = getConnection(dbName);
    Database db = (Database) ((JdbcConnection) conn).getSession().getDataHandler();
    assertTrue(db.getMvStore().getStore().getFileStore().isReadOnly());
    conn.close();
}
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)

Example 8 with JdbcConnection

use of org.h2.jdbc.JdbcConnection 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();
}
Also used : RandomGen(org.h2.test.synth.sql.RandomGen) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) JdbcConnection(org.h2.jdbc.JdbcConnection) Method(java.lang.reflect.Method) Savepoint(java.sql.Savepoint)

Example 9 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project h2database by h2database.

the class TestDiskSpaceLeak method main.

/**
 * Run just this test.
 *
 * @param args ignored
 */
public static void main(String... args) throws Exception {
    DeleteDbFiles.execute("data", null, true);
    Class.forName("org.h2.Driver");
    Connection conn;
    long before = 0;
    for (int i = 0; i < 10; i++) {
        conn = DriverManager.getConnection("jdbc:h2:data/test");
        ResultSet rs;
        rs = conn.createStatement().executeQuery("select count(*) from information_schema.lobs");
        rs.next();
        System.out.println("lobs: " + rs.getInt(1));
        rs = conn.createStatement().executeQuery("select count(*) from information_schema.lob_map");
        rs.next();
        System.out.println("lob_map: " + rs.getInt(1));
        rs = conn.createStatement().executeQuery("select count(*) from information_schema.lob_data");
        rs.next();
        System.out.println("lob_data: " + rs.getInt(1));
        conn.close();
        Recover.execute("data", "test");
        new File("data/test.h2.sql").renameTo(new File("data/test." + i + ".sql"));
        conn = DriverManager.getConnection("jdbc:h2:data/test");
        // ((JdbcConnection) conn).setPowerOffCount(i);
        ((JdbcConnection) conn).setPowerOffCount(28);
        String last = "connect";
        try {
            conn.createStatement().execute("drop table test if exists");
            last = "drop";
            conn.createStatement().execute("create table test(id identity, b blob)");
            last = "create";
            conn.createStatement().execute("insert into test values(1, space(10000))");
            last = "insert";
            conn.createStatement().execute("delete from test");
            last = "delete";
            conn.createStatement().execute("insert into test values(1, space(10000))");
            last = "insert2";
            conn.createStatement().execute("delete from test");
            last = "delete2";
        } catch (SQLException e) {
        // ignore
        } finally {
            JdbcUtils.closeSilently(conn);
        }
        long now = new File("data/test.h2.db").length();
        long diff = now - before;
        before = now;
        System.out.println(now + " " + diff + " " + i + " " + last);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) JdbcConnection(org.h2.jdbc.JdbcConnection) File(java.io.File)

Example 10 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project h2database by h2database.

the class JdbcBlob method setBinaryStream.

/**
 * Get a writer to update the Blob. This is only supported for new, empty
 * Blob objects that were created with Connection.createBlob(). The Blob is
 * created in a separate thread, and the object is only updated when
 * OutputStream.close() is called. The position must be 1, meaning the whole
 * Blob data is set.
 *
 * @param pos where to start writing (the first byte is at position 1)
 * @return an output stream
 */
@Override
public OutputStream setBinaryStream(long pos) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("setBinaryStream(" + pos + ");");
        }
        checkClosed();
        if (pos != 1) {
            throw DbException.getInvalidValueException("pos", pos);
        }
        if (value.getPrecision() != 0) {
            throw DbException.getInvalidValueException("length", value.getPrecision());
        }
        // local variable avoids generating synthetic accessor method
        final JdbcConnection c = conn;
        final PipedInputStream in = new PipedInputStream();
        final Task task = new Task() {

            @Override
            public void call() {
                value = c.createBlob(in, -1);
            }
        };
        PipedOutputStream out = new PipedOutputStream(in) {

            @Override
            public void close() throws IOException {
                super.close();
                try {
                    task.get();
                } catch (Exception e) {
                    throw DbException.convertToIOException(e);
                }
            }
        };
        task.execute();
        return new BufferedOutputStream(out);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Task(org.h2.util.Task) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

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