use of io.bootique.jdbc.managed.ManagedDataSourceSupplier in project bootique-jdbc by bootique.
the class HikariCPManagedDataSourceFactory method create.
@Override
public Optional<ManagedDataSourceSupplier> create(String dataSourceName, Injector injector) {
if (jdbcUrl == null) {
return Optional.empty();
}
Supplier<DataSource> startup = () -> {
validate();
HikariConfig hikariConfig = toConfiguration();
return new HikariDataSource(hikariConfig);
};
Consumer<DataSource> shutdown = ds -> ((HikariDataSource) ds).close();
return Optional.of(new ManagedDataSourceSupplier(getJdbcUrl(), startup, shutdown));
}
use of io.bootique.jdbc.managed.ManagedDataSourceSupplier in project bootique-jdbc by bootique.
the class HikariCPInstrumentedDataSourceFactory method create.
@Override
public Optional<ManagedDataSourceSupplier> create(String dataSourceName, Injector injector) {
if (getJdbcUrl() == null) {
return Optional.empty();
}
Supplier<DataSource> startup = () -> {
validate();
HikariConfig hikariConfig = toConfiguration();
HikariDataSource ds = new HikariDataSource(hikariConfig);
this.addMetrics(ds, injector);
this.addHealthChecks(ds, dataSourceName, injector);
return ds;
};
Consumer<DataSource> shutdown = ds -> ((HikariDataSource) ds).close();
return Optional.of(new ManagedDataSourceSupplier(getJdbcUrl(), startup, shutdown));
}
use of io.bootique.jdbc.managed.ManagedDataSourceSupplier in project bootique-jdbc by bootique.
the class TomcatManagedDataSourceFactory method create.
@Override
public Optional<ManagedDataSourceSupplier> create(String dataSourceName, Injector injector) {
// Once we stop supporting vars based on naming conventions, we can replace Optional<T> with just T
if (url == null) {
return Optional.empty();
}
Supplier<javax.sql.DataSource> startup = () -> {
validate();
PoolConfiguration poolConfig = toConfiguration();
org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(poolConfig);
try {
dataSource.createPool();
} catch (Exception e) {
throw new RuntimeException("Error creating DataSource", e);
}
return dataSource;
};
Consumer<javax.sql.DataSource> shutdown = ds -> ((org.apache.tomcat.jdbc.pool.DataSource) ds).close();
return Optional.of(new ManagedDataSourceSupplier(getUrl(), startup, shutdown));
}
use of io.bootique.jdbc.managed.ManagedDataSourceSupplier in project bootique-jdbc by bootique.
the class LazyDataSourceFactory method createManagedDataSource.
protected ManagedDataSource createManagedDataSource(String name) {
ManagedDataSourceSupplier factory = dataSourceFactories.get(name);
if (factory == null) {
throw new IllegalStateException("No configuration present for DataSource named '" + name + "'");
}
String url = factory.getUrl();
listeners.forEach(listener -> listener.beforeStartup(name, url));
ManagedDataSource dataSource = factory.start();
listeners.forEach(listener -> listener.afterStartup(name, url, dataSource.getDataSource()));
return dataSource;
}
use of io.bootique.jdbc.managed.ManagedDataSourceSupplier in project bootique-jdbc by bootique.
the class LazyDataSourceFactoryTest method testAllNames.
@Test
public void testAllNames() {
LazyDataSourceFactory f1 = new LazyDataSourceFactory(Collections.emptyMap(), Collections.emptySet());
assertEquals(0, f1.allNames().size());
ManagedDataSourceSupplier factory = mock(ManagedDataSourceSupplier.class);
LazyDataSourceFactory f2 = new LazyDataSourceFactory(Collections.singletonMap("a", factory), Collections.emptySet());
assertEquals(1, f2.allNames().size());
assertTrue(f2.allNames().contains("a"));
}
Aggregations