io.dropwizard.db.ManagedDataSource Java Examples

The following examples show how to use io.dropwizard.db.ManagedDataSource. 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: DBIFactory.java    From dropwizard-java8 with Apache License 2.0 6 votes vote down vote up
@Override
public DBI build(Environment environment,
                 PooledDataSourceFactory configuration,
                 ManagedDataSource dataSource,
                 String name) {
    final DBI dbi = super.build(environment, configuration, dataSource, name);

    dbi.registerArgumentFactory(new OptionalArgumentFactory(configuration.getDriverClass()));
    dbi.registerContainerFactory(new OptionalContainerFactory());
    dbi.registerArgumentFactory(new LocalDateArgumentFactory());
    dbi.registerArgumentFactory(new OptionalLocalDateArgumentFactory());
    dbi.registerArgumentFactory(new LocalDateTimeArgumentFactory());
    dbi.registerArgumentFactory(new OptionalLocalDateTimeArgumentFactory());
    dbi.registerMapper(new LocalDateMapper());
    dbi.registerMapper(new LocalDateTimeMapper());

    final Optional<TimeZone> tz = Optional.ofNullable(databaseTimeZone().orNull());
    dbi.registerArgumentFactory(new InstantArgumentFactory(tz));
    dbi.registerArgumentFactory(new OptionalInstantArgumentFactory(tz));
    dbi.registerMapper(new InstantMapper(tz));


    return dbi;
}
 
Example #2
Source File: EventStoreFactory.java    From cqrs-eventsourcing-kafka with Apache License 2.0 5 votes vote down vote up
private EventStore buildJdbcEventStore(EventPublisher publisher, Environment environment) {
    ManagedDataSource ds = database.build(environment.metrics(), "eventstore");
    DBI jdbi = new DBIFactory().build(environment, database, "eventstore");
    updateDatabaseSchema(ds);
    EventStore eventStore = new JdbcEventStore(jdbi, environment.getObjectMapper(), publisher);
    return eventStore;
}
 
Example #3
Source File: ServiceModule.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
@Provides @Singleton @Readonly ManagedDataSource readonlyDataSource(Environment environment,
    KeywhizConfig config) {
  DataSourceFactory dataSourceFactory = config.getReadonlyDataSourceFactory();
  ManagedDataSource dataSource = dataSourceFactory.build(environment.metrics(), "db-readonly");
  environment.lifecycle().manage(dataSource);

  environment.healthChecks().register("db-readonly-health",
      new JooqHealthCheck(dataSource, RETURN_UNHEALTHY));

  return dataSource;
}
 
Example #4
Source File: ServiceModule.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
@Provides @Singleton
@Readonly DSLContext readonlyJooqContext(@Readonly ManagedDataSource dataSource)
    throws SQLException {
  DSLContext dslContext = DSLContexts.databaseAgnostic(dataSource);
  org.jooq.Configuration configuration = dslContext.configuration();
  // Disable support for nested transactions via savepoints (required for MySQL)
  // See: https://groups.google.com/forum/#!topic/jooq-user/zG0U6CkxI5o
  configuration.set(new DefaultTransactionProvider(configuration.connectionProvider(), false));
  return dslContext;
}
 
Example #5
Source File: JooqFactory.java    From droptools with Apache License 2.0 5 votes vote down vote up
public Configuration build(Environment environment, PooledDataSourceFactory factory, String name) throws ClassNotFoundException {
    final Settings settings = buildSettings();
    final ManagedDataSource dataSource = factory.build(environment.metrics(), name);
    final SQLDialect dialect = determineDialect(factory, dataSource);
    final ConnectionProvider connectionProvider = new DataSourceConnectionProvider(dataSource);
    final Configuration config = new DefaultConfiguration()
            .set(settings)
            .set(dialect)
            .set(connectionProvider);

    environment.lifecycle().manage(dataSource);

    return config;
}
 
Example #6
Source File: JooqFactory.java    From droptools with Apache License 2.0 5 votes vote down vote up
private SQLDialect determineDialect(PooledDataSourceFactory dataSourceFactory, ManagedDataSource dataSource) {
    // If a dialect was specified, great!
    if (getDialect().isPresent()) {
        return dialect.get();
    }

    return JDBCUtils.dialect(dataSourceFactory.getUrl());
}
 
Example #7
Source File: KeywhizConfig.java    From keywhiz with Apache License 2.0 4 votes vote down vote up
@Override public ManagedDataSource build(MetricRegistry metricRegistry, String name) {
  setPassword(getPassword());
  return super.build(metricRegistry, name);
}
 
Example #8
Source File: ServiceModule.java    From keywhiz with Apache License 2.0 4 votes vote down vote up
@Provides @Singleton DSLContext jooqContext(ManagedDataSource dataSource) throws SQLException {
  return DSLContexts.databaseAgnostic(dataSource);
}
 
Example #9
Source File: DbDiffCommand.java    From dropwizard-experiment with MIT License 4 votes vote down vote up
private static CloseableLiquibase createLiquibase(DataSourceFactory dbConfig) throws SQLException, LiquibaseException {
    ManagedDataSource dataSource = dbConfig.build(new MetricRegistry(), "liquibase");
    return new CloseableLiquibase(dataSource);
}
 
Example #10
Source File: JooqHealthCheck.java    From keywhiz with Apache License 2.0 2 votes vote down vote up
/**
 * The constructor takes a ManagedDataSource instead of a DSLContext so that we can catch and
 * handle any SQLException thrown by DSL.using.
 *
 * @param dataSource connection to monitor
 * @param onFailure allows us to log but report a connection as healthy. Useful for allowing
 * operations in degraded mode.
 */
public JooqHealthCheck(ManagedDataSource dataSource, OnFailure onFailure) {
  this.dataSource = dataSource;
  this.onFailure = onFailure;
}