Java Code Examples for org.h2.jdbc.JdbcConnection

The following examples show how to use org.h2.jdbc.JdbcConnection. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: glowroot   Source File: DataSource.java    License: Apache License 2.0 6 votes vote down vote up
private static JdbcConnection createConnection(@Nullable File dbFile) throws SQLException {
    if (dbFile == null) {
        // db_close_on_exit=false since jvm shutdown hook is handled by DataSource
        return new JdbcConnection("jdbc:h2:mem:;compress=true;db_close_on_exit=false",
                new Properties());
    } else {
        String dbPath = dbFile.getPath();
        dbPath = dbPath.replaceFirst(".h2.db$", "");
        Properties props = new Properties();
        props.setProperty("user", "sa");
        props.setProperty("password", "");
        // db_close_on_exit=false since jvm shutdown hook is handled by DataSource
        String url = "jdbc:h2:" + dbPath + ";compress=true;db_close_on_exit=false;cache_size="
                + CACHE_SIZE;
        return new JdbcConnection(url, props);
    }
}
 
Example 2
Source Project: glowroot   Source File: SchemasTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReadIndexes() throws Exception {
    // given
    Connection connection = new JdbcConnection("jdbc:h2:mem:", new Properties());
    Statement statement = connection.createStatement();
    statement.execute("create table tab (a varchar, b bigint)");
    statement.execute("create index tab_idx on tab (a)");
    // when
    Set<Index> indexes = Schemas.getIndexes("tab", connection);
    // then
    assertThat(indexes).hasSize(1);
    assertThat(indexes.iterator().next())
            .isEqualTo(ImmutableIndex.of("tab_idx", ImmutableList.of("a")));
}
 
Example 3
Source Project: clearpool   Source File: H2Utils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static XAConnection createXAConnection(Object factory, Connection physicalConn)
    throws SQLException {
  try {
    if (constructor == null) {
      constructor = JdbcXAConnection.class.getDeclaredConstructor(JdbcDataSourceFactory.class,
          int.class, JdbcConnection.class);
      constructor.setAccessible(true);
    }
    int id = getNextId(XA_DATA_SOURCE);
    return constructor.newInstance(factory, id, physicalConn);
  } catch (Exception e) {
    throw new SQLException("createXAConnection error", e);
  }
}
 
Example 4
Source Project: presto   Source File: DatabaseShardManager.java    License: Apache License 2.0 4 votes vote down vote up
private static int batchSize(Connection connection)
{
    // H2 does not return generated keys properly
    // https://github.com/h2database/h2database/issues/156
    return (connection instanceof JdbcConnection) ? 1 : 1000;
}
 
Example 5
Source Project: shardingsphere   Source File: H2XAConnectionWrapper.java    License: Apache License 2.0 4 votes vote down vote up
@SneakyThrows
private static Constructor<JdbcXAConnection> getH2JdbcXAConstructor() {
    Constructor<JdbcXAConnection> result = JdbcXAConnection.class.getDeclaredConstructor(JdbcDataSourceFactory.class, Integer.TYPE, JdbcConnection.class);
    result.setAccessible(true);
    return result;
}
 
Example 6
Source Project: shardingsphere   Source File: H2XAConnectionWrapper.java    License: Apache License 2.0 4 votes vote down vote up
@SneakyThrows
@Override
public XAConnection wrap(final XADataSource xaDataSource, final Connection connection) {
    Connection physicalConnection = connection.unwrap(JdbcConnection.class);
    return CONSTRUCTOR.newInstance(FACTORY, NEXT_ID.invoke(null, XA_DATA_SOURCE), physicalConnection);
}
 
Example 7
Source Project: glowroot   Source File: Connections.java    License: Apache License 2.0 4 votes vote down vote up
private static Connection createH2Connection() throws SQLException {
    Connection connection =
            new JdbcConnection("jdbc:h2:mem:;db_close_on_exit=false", new Properties());
    insertRecords(connection);
    return connection;
}
 
Example 8
Source Project: ignite   Source File: H2Utils.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * @param c Connection.
 * @return Session.
 */
public static Session session(Connection c) {
    return (Session)((JdbcConnection)c).getSession();
}