Search in sources :

Example 71 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class AnnotationsTest method testCreateTableIfRequiredAnnotation.

private void testCreateTableIfRequiredAnnotation() {
    // tests JQTable.createTableIfRequired=false
    Db noCreateDb = null;
    try {
        noCreateDb = Db.open("jdbc:h2:mem:", "sa", "sa");
        noCreateDb.insertAll(ProductNoCreateTable.getList());
        noCreateDb.close();
    } catch (RuntimeException r) {
        SQLException s = (SQLException) r.getCause();
        assertEquals(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s.getErrorCode());
    }
    if (noCreateDb != null) {
        JdbcUtils.closeSilently(noCreateDb.getConnection());
    }
}
Also used : SQLException(java.sql.SQLException) Db(org.h2.jaqu.Db)

Example 72 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TestLob method testLobCompression.

private void testLobCompression(boolean compress) throws Exception {
    deleteDb("lob");
    Connection conn;
    conn = reconnect(null);
    if (compress) {
        conn.createStatement().execute("SET COMPRESS_LOB LZF");
    } else {
        conn.createStatement().execute("SET COMPRESS_LOB NO");
    }
    conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, C CLOB)");
    PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
    long time = System.nanoTime();
    int len = getSize(10, 40);
    if (config.networked && config.big) {
        len = 5;
    }
    StringBuilder buff = new StringBuilder();
    for (int i = 0; i < 1000; i++) {
        buff.append(StringUtils.xmlNode("content", null, "This is a test " + i));
    }
    String xml = buff.toString();
    for (int i = 0; i < len; i++) {
        prep.setInt(1, i);
        prep.setString(2, xml + i);
        prep.execute();
    }
    for (int i = 0; i < len; i++) {
        ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST");
        while (rs.next()) {
            if (i == 0) {
                assertEquals(xml + rs.getInt(1), rs.getString(2));
            } else {
                Reader r = rs.getCharacterStream(2);
                String result = IOUtils.readStringAndClose(r, -1);
                assertEquals(xml + rs.getInt(1), result);
            }
        }
    }
    time = System.nanoTime() - time;
    trace("time: " + TimeUnit.NANOSECONDS.toMillis(time) + " compress: " + compress);
    conn.close();
    if (!config.memory) {
        long length = new File(getBaseDir() + "/lob.h2.db").length();
        trace("len: " + length + " compress: " + compress);
    }
}
Also used : Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) StringReader(java.io.StringReader) PreparedStatement(java.sql.PreparedStatement) File(java.io.File) Savepoint(java.sql.Savepoint)

Example 73 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TestMultiThread method testConcurrentLobAdd.

private void testConcurrentLobAdd() throws Exception {
    String db = getTestName();
    deleteDb(db);
    final String url = getURL(db + ";MULTI_THREADED=1", true);
    try (Connection conn = getConnection(url)) {
        Statement stat = conn.createStatement();
        stat.execute("create table test(id identity, data clob)");
        Task[] tasks = new Task[2];
        for (int i = 0; i < tasks.length; i++) {
            Task t = new Task() {

                @Override
                public void call() throws Exception {
                    try (Connection c2 = getConnection(url)) {
                        PreparedStatement p2 = c2.prepareStatement("insert into test(data) values(?)");
                        while (!stop) {
                            p2.setCharacterStream(1, new StringReader(new String(new char[10 * 1024])));
                            p2.execute();
                        }
                    }
                }
            };
            tasks[i] = t;
            t.execute();
        }
        Thread.sleep(500);
        for (Task t : tasks) {
            t.get();
        }
    }
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) StringReader(java.io.StringReader) PreparedStatement(java.sql.PreparedStatement)

Example 74 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TestMultiThread method testConcurrentView.

private void testConcurrentView() throws Exception {
    if (config.mvcc || config.mvStore) {
        return;
    }
    String db = getTestName();
    deleteDb(db);
    final String url = getURL(db + ";MULTI_THREADED=1", true);
    final Random r = new Random();
    try (Connection conn = getConnection(url)) {
        Statement stat = conn.createStatement();
        StringBuilder buff = new StringBuilder();
        buff.append("create table test(id int");
        final int len = 3;
        for (int i = 0; i < len; i++) {
            buff.append(", x" + i + " int");
        }
        buff.append(")");
        stat.execute(buff.toString());
        stat.execute("create view test_view as select * from test");
        stat.execute("insert into test(id) select x from system_range(1, 2)");
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                Connection c2 = getConnection(url);
                while (!stop) {
                    c2.prepareStatement("select * from test_view where x" + r.nextInt(len) + "=1");
                }
                c2.close();
            }
        };
        t.execute();
        SynchronizedVerifier.setDetect(SmallLRUCache.class, true);
        for (int i = 0; i < 1000; i++) {
            conn.prepareStatement("select * from test_view where x" + r.nextInt(len) + "=1");
        }
        t.get();
        SynchronizedVerifier.setDetect(SmallLRUCache.class, false);
    }
}
Also used : Task(org.h2.util.Task) Random(java.util.Random) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 75 with Db

use of org.h2.jaqu.Db in project jdbi by jdbi.

the class TestMixinInterfaces method setUp.

@Before
public void setUp() throws Exception {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID()));
    db = Jdbi.create(ds);
    db.installPlugin(new SqlObjectPlugin());
    handle = db.open();
    handle.execute("create table something (id int primary key, name varchar(100))");
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Before(org.junit.Before)

Aggregations

Database (org.h2.engine.Database)70 Connection (java.sql.Connection)31 Statement (java.sql.Statement)20 Table (org.h2.table.Table)19 PreparedStatement (java.sql.PreparedStatement)18 ResultSet (java.sql.ResultSet)13 SQLException (java.sql.SQLException)13 Column (org.h2.table.Column)12 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)9 StatementBuilder (org.h2.util.StatementBuilder)9 DbObject (org.h2.engine.DbObject)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 DbException (org.h2.message.DbException)7 Schema (org.h2.schema.Schema)7 Before (org.junit.Before)7 InputStream (java.io.InputStream)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 JdbcConnection (org.h2.jdbc.JdbcConnection)6