Search in sources :

Example 1 with ManagedDataSource

use of io.dropwizard.db.ManagedDataSource in project keywhiz by square.

the class ServiceModule method readonlyDataSource.

@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;
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) ManagedDataSource(io.dropwizard.db.ManagedDataSource) Readonly(keywhiz.service.config.Readonly) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 2 with ManagedDataSource

use of io.dropwizard.db.ManagedDataSource in project keywhiz by square.

the class ServiceModule method dataSource.

// ManagedDataSource
@Provides
@Singleton
ManagedDataSource dataSource(Environment environment, KeywhizConfig config) {
    DataSourceFactory dataSourceFactory = config.getDataSourceFactory();
    ManagedDataSource dataSource = dataSourceFactory.build(environment.metrics(), "db-writable");
    environment.lifecycle().manage(dataSource);
    environment.healthChecks().register("db-read-write-health", new JooqHealthCheck(dataSource, LOG_ONLY));
    return dataSource;
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) ManagedDataSource(io.dropwizard.db.ManagedDataSource) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 3 with ManagedDataSource

use of io.dropwizard.db.ManagedDataSource in project dropwizard by dropwizard.

the class JdbiFactoryTest method testBuild.

@Test
void testBuild() {
    final Environment environment = mock(Environment.class);
    final MetricRegistry metrics = mock(MetricRegistry.class);
    final LifecycleEnvironment lifecycle = mock(LifecycleEnvironment.class);
    final HealthCheckRegistry healthChecks = mock(HealthCheckRegistry.class);
    final PooledDataSourceFactory configuration = mock(PooledDataSourceFactory.class);
    final String name = UUID.randomUUID().toString();
    final ManagedDataSource dataSource = mock(ManagedDataSource.class);
    final String validationQuery = UUID.randomUUID().toString();
    final Jdbi jdbi = mock(Jdbi.class);
    final SqlStatements sqlStatements = new SqlStatements();
    when(environment.metrics()).thenReturn(metrics);
    when(environment.lifecycle()).thenReturn(lifecycle);
    when(environment.healthChecks()).thenReturn(healthChecks);
    when(configuration.build(metrics, name)).thenReturn(dataSource);
    when(configuration.getValidationQuery()).thenReturn(Optional.of(validationQuery));
    when(configuration.isAutoCommentsEnabled()).thenReturn(true);
    when(jdbi.getConfig(SqlStatements.class)).thenReturn(sqlStatements);
    final JdbiFactory factory = spy(new JdbiFactory());
    when(factory.newInstance(dataSource)).thenReturn(jdbi);
    final Jdbi result = factory.build(environment, configuration, name);
    assertThat(result).isSameAs(jdbi);
    verify(lifecycle).manage(dataSource);
    verify(healthChecks).register(eq(name), any(JdbiHealthCheck.class));
    verify(jdbi).setSqlLogger(any(InstrumentedSqlLogger.class));
    verify(factory).buildSQLLogger(same(metrics), any(StatementNameStrategy.class));
    verify(jdbi).setTemplateEngine(any(NamePrependingTemplateEngine.class));
    verify(factory).configure(jdbi);
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) StatementNameStrategy(com.codahale.metrics.jdbi3.strategies.StatementNameStrategy) MetricRegistry(com.codahale.metrics.MetricRegistry) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) InstrumentedSqlLogger(com.codahale.metrics.jdbi3.InstrumentedSqlLogger) SqlStatements(org.jdbi.v3.core.statement.SqlStatements) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Environment(io.dropwizard.setup.Environment) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) PooledDataSourceFactory(io.dropwizard.db.PooledDataSourceFactory) ManagedDataSource(io.dropwizard.db.ManagedDataSource) Test(org.junit.jupiter.api.Test)

Example 4 with ManagedDataSource

use of io.dropwizard.db.ManagedDataSource in project dropwizard by dropwizard.

the class AbstractLiquibaseCommand method openLiquibase.

CloseableLiquibase openLiquibase(final PooledDataSourceFactory dataSourceFactory, final Namespace namespace) throws SQLException, LiquibaseException {
    final CloseableLiquibase liquibase;
    final ManagedDataSource dataSource = dataSourceFactory.build(new MetricRegistry(), "liquibase");
    final Database database = createDatabase(dataSource, namespace);
    final String migrationsFile = namespace.getString("migrations-file");
    if (migrationsFile == null) {
        liquibase = new CloseableLiquibaseWithClassPathMigrationsFile(dataSource, database, migrationsFileName);
    } else {
        liquibase = new CloseableLiquibaseWithFileSystemMigrationsFile(dataSource, database, migrationsFile);
    }
    return liquibase;
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Database(liquibase.database.Database) ManagedDataSource(io.dropwizard.db.ManagedDataSource)

Example 5 with ManagedDataSource

use of io.dropwizard.db.ManagedDataSource in project keywhiz by square.

the class DataSourceModuleTest method injectsDataSources.

@Test
public void injectsDataSources() {
    class Holder {

        @Inject
        ManagedDataSource readWrite;

        @Inject
        @Readonly
        ManagedDataSource readonly;
    }
    Holder holder = new Holder();
    ManagedDataSource readWrite = mock(ManagedDataSource.class);
    ManagedDataSource readonly = mock(ManagedDataSource.class);
    Guice.createInjector(new DataSourceModule(readWrite, readonly)).injectMembers(holder);
    assertSame(readWrite, holder.readWrite);
    assertSame(readonly, holder.readonly);
}
Also used : ManagedDataSource(io.dropwizard.db.ManagedDataSource) Test(org.junit.Test)

Aggregations

ManagedDataSource (io.dropwizard.db.ManagedDataSource)12 MetricRegistry (com.codahale.metrics.MetricRegistry)5 Injector (com.google.inject.Injector)4 Provides (com.google.inject.Provides)4 Singleton (com.google.inject.Singleton)4 DataSourceFactory (io.dropwizard.db.DataSourceFactory)4 Readonly (keywhiz.service.config.Readonly)3 Test (org.junit.Test)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 JooqHealthCheck (keywhiz.JooqHealthCheck)2 ServiceDataSourceModule (keywhiz.ServiceDataSourceModule)2 ContextModule (keywhiz.inject.ContextModule)2 ServiceContext (keywhiz.test.ServiceContext)2 HealthCheckRegistry (com.codahale.metrics.health.HealthCheckRegistry)1 InstrumentedSqlLogger (com.codahale.metrics.jdbi3.InstrumentedSqlLogger)1 StatementNameStrategy (com.codahale.metrics.jdbi3.strategies.StatementNameStrategy)1 PooledDataSourceFactory (io.dropwizard.db.PooledDataSourceFactory)1 LifecycleEnvironment (io.dropwizard.lifecycle.setup.LifecycleEnvironment)1 Environment (io.dropwizard.setup.Environment)1 Duration (java.time.Duration)1