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;
}
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;
}
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);
}
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;
}
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);
}
Aggregations