Search in sources :

Example 31 with JdbcConnection

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

the class ShowProgress method test.

/**
 * Run the progress test.
 */
void test() throws Exception {
    Class.forName("org.h2.Driver");
    Connection conn = DriverManager.getConnection("jdbc:h2:test", "sa", "");
    Statement stat = conn.createStatement();
    stat.execute("DROP TABLE IF EXISTS TEST");
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
    PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, 'Test' || SPACE(100))");
    long time;
    time = System.nanoTime();
    int len = 1000;
    for (int i = 0; i < len; i++) {
        long now = System.nanoTime();
        if (now > time + TimeUnit.SECONDS.toNanos(1)) {
            time = now;
            System.out.println("Inserting " + (100L * i / len) + "%");
        }
        prep.setInt(1, i);
        prep.execute();
    }
    boolean abnormalTermination = true;
    if (abnormalTermination) {
        ((JdbcConnection) conn).setPowerOffCount(1);
        try {
            stat.execute("INSERT INTO TEST VALUES(-1, 'Test' || SPACE(100))");
        } catch (SQLException e) {
        // ignore
        }
    } else {
        conn.close();
    }
    System.out.println("Open connection...");
    time = System.nanoTime();
    conn = DriverManager.getConnection("jdbc:h2:test;DATABASE_EVENT_LISTENER='" + getClass().getName() + "'", "sa", "");
    time = System.nanoTime() - time;
    System.out.println("Done after " + TimeUnit.NANOSECONDS.toMillis(time) + " ms");
    prep.close();
    stat.close();
    conn.close();
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 32 with JdbcConnection

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

the class TestView method testSubQueryViewIndexCache.

private void testSubQueryViewIndexCache() throws SQLException {
    if (config.networked) {
        return;
    }
    Connection conn = getConnection("view");
    Statement stat = conn.createStatement();
    stat.execute("drop table test if exists");
    stat.execute("create table test(id int primary key, " + "name varchar(25) unique, age int unique)");
    // check that initial cache size is empty
    Session s = (Session) ((JdbcConnection) conn).getSession();
    s.clearViewIndexCache();
    assertTrue(s.getViewIndexCache(true).isEmpty());
    assertTrue(s.getViewIndexCache(false).isEmpty());
    // create view command should not affect caches
    stat.execute("create view v as select * from test");
    assertTrue(s.getViewIndexCache(true).isEmpty());
    assertTrue(s.getViewIndexCache(false).isEmpty());
    // check view index cache
    stat.executeQuery("select * from v where id > 0").next();
    int size1 = s.getViewIndexCache(false).size();
    assertTrue(size1 > 0);
    assertTrue(s.getViewIndexCache(true).isEmpty());
    stat.executeQuery("select * from v where name = 'xyz'").next();
    int size2 = s.getViewIndexCache(false).size();
    assertTrue(size2 > size1);
    assertTrue(s.getViewIndexCache(true).isEmpty());
    // check we did not add anything to view cache if we run a sub-query
    stat.executeQuery("select * from (select * from test) where age = 17").next();
    int size3 = s.getViewIndexCache(false).size();
    assertEquals(size2, size3);
    assertTrue(s.getViewIndexCache(true).isEmpty());
    // check clear works
    s.clearViewIndexCache();
    assertTrue(s.getViewIndexCache(false).isEmpty());
    assertTrue(s.getViewIndexCache(true).isEmpty());
    // drop everything
    stat.execute("drop view v");
    stat.execute("drop table test");
    conn.close();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) Session(org.h2.engine.Session)

Example 33 with JdbcConnection

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

the class TestTableEngines method setBatchingEnabled.

private void setBatchingEnabled(Statement stat, boolean enabled) throws SQLException {
    stat.execute("SET BATCH_JOINS " + enabled);
    if (!config.networked) {
        Session s = (Session) ((JdbcConnection) stat.getConnection()).getSession();
        assertEquals(enabled, s.isJoinBatchEnabled());
    }
}
Also used : Session(org.h2.engine.Session)

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