Search in sources :

Example 1 with AgroalDataSourceConfiguration

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);
        }
    }
}
Also used : SQLException(java.sql.SQLException) AgroalDataSourceConfiguration(io.agroal.api.configuration.AgroalDataSourceConfiguration) AgroalDataSource(io.agroal.api.AgroalDataSource) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) AgroalPropertiesReader(io.agroal.api.configuration.supplier.AgroalPropertiesReader) NamingException(javax.naming.NamingException) IndyLifecycleException(org.commonjava.indy.action.IndyLifecycleException)

Aggregations

AgroalDataSource (io.agroal.api.AgroalDataSource)1 AgroalDataSourceConfiguration (io.agroal.api.configuration.AgroalDataSourceConfiguration)1 AgroalPropertiesReader (io.agroal.api.configuration.supplier.AgroalPropertiesReader)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 IndyLifecycleException (org.commonjava.indy.action.IndyLifecycleException)1