org.h2.jdbc.JdbcConnection Java Examples

The following examples show how to use org.h2.jdbc.JdbcConnection. 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 File: DataSource.java    From glowroot with 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 File: SchemasTest.java    From glowroot with 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 File: H2Utils.java    From clearpool with 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 File: DatabaseShardManager.java    From presto with 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 File: H2XAConnectionWrapper.java    From shardingsphere with 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 File: H2XAConnectionWrapper.java    From shardingsphere with 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 File: Connections.java    From glowroot with 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 File: H2Utils.java    From ignite with 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();
}