Search in sources :

Example 11 with ManagedDataSource

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

the class ServiceDataSourceModule method dataSource.

@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 : JooqHealthCheck(keywhiz.JooqHealthCheck) DataSourceFactory(io.dropwizard.db.DataSourceFactory) ManagedDataSource(io.dropwizard.db.ManagedDataSource) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 12 with ManagedDataSource

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

the class ServiceDataSourceModuleTest method dataSourcesAreSingletons.

@Test
public void dataSourcesAreSingletons() {
    class Holder {

        @Inject
        ManagedDataSource readWrite;

        @Inject
        @Readonly
        ManagedDataSource readonly;
    }
    Holder holder1 = new Holder();
    Holder holder2 = new Holder();
    ServiceContext context = ServiceContext.create();
    Injector injector = Guice.createInjector(new ContextModule(context.getConfig(), context.getEnvironment()), new ServiceDataSourceModule());
    injector.injectMembers(holder1);
    injector.injectMembers(holder2);
    assertSame(holder1.readWrite, holder2.readWrite);
    assertSame(holder1.readonly, holder2.readonly);
}
Also used : ServiceContext(keywhiz.test.ServiceContext) Injector(com.google.inject.Injector) ContextModule(keywhiz.inject.ContextModule) ServiceDataSourceModule(keywhiz.ServiceDataSourceModule) 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