use of io.agroal.api.configuration.AgroalDataSourceConfiguration in project indy by Commonjava.
the class ConnectionPoolProvider method init.
public void init() throws IndyLifecycleException {
logger.info("Starting connection pool binding...");
Properties properties = System.getProperties();
properties.setProperty(INITIAL_CONTEXT_FACTORY, CPInitialContextFactory.class.getName());
System.setProperties(properties);
InitialContext ctx;
try {
ctx = new InitialContext();
} catch (NamingException e) {
throw new IndyLifecycleException("Failed to create JNDI InitialContext for binding datasources", e);
}
Map<String, ConnectionPoolInfo> poolConfigs = config.getPools();
logger.info("Creating bindings for {} pools from config: {}", poolConfigs.size(), config);
for (ConnectionPoolInfo poolInfo : poolConfigs.values()) {
try {
AgroalPropertiesReader propertiesReader = new AgroalPropertiesReader(ConnectionPoolConfig.DS_PROPERTY_PREFIX);
AgroalDataSourceConfiguration config = propertiesReader.readProperties(poolInfo.getProperties()).get();
config.setMetricsEnabled(poolInfo.isUseMetrics());
AgroalDataSource ds = AgroalDataSource.from(config, new AgroalDataSourceLogger(poolInfo.getName()));
if (poolInfo.isUseMetrics()) {
registerMetrics(ds.getMetrics(), poolInfo.getName());
}
if (poolInfo.isUseHealthChecks()) {
registerHealthChecks(ds, poolInfo.getName());
}
String jndiName = "java:/comp/env/jdbc/" + poolInfo.getName();
logger.info("Binding datasource: {}", jndiName);
ctx.rebind(jndiName, ds);
} catch (NamingException e) {
throw new IndyLifecycleException("Failed to bind datasource: " + poolInfo.getName(), e);
} catch (SQLException e) {
throw new IndyLifecycleException("Failed to start datasource: " + poolInfo.getName(), e);
}
}
}
Aggregations