Search in sources :

Example 1 with AgroalPropertiesReader

use of io.agroal.api.configuration.supplier.AgroalPropertiesReader in project hibernate-orm by hibernate.

the class AgroalConnectionProvider method configure.

@Override
@SuppressWarnings("unchecked")
public void configure(Map props) throws HibernateException {
    LOGGER.debug("Configuring Agroal");
    try {
        AgroalPropertiesReader agroalProperties = new AgroalPropertiesReader(CONFIG_PREFIX).readProperties(props);
        agroalProperties.modify().connectionPoolConfiguration(cp -> cp.connectionFactoryConfiguration(cf -> {
            copyProperty(props, AvailableSettings.DRIVER, cf::driverClassName, Function.identity());
            copyProperty(props, AvailableSettings.URL, cf::jdbcUrl, Function.identity());
            copyProperty(props, AvailableSettings.USER, cf::principal, NamePrincipal::new);
            copyProperty(props, AvailableSettings.PASS, cf::credential, SimplePassword::new);
            copyProperty(props, AvailableSettings.AUTOCOMMIT, cf::autoCommit, Boolean::valueOf);
            resolveIsolationSetting(props, cf);
            return cf;
        }));
        agroalDataSource = AgroalDataSource.from(agroalProperties);
    } catch (Exception e) {
        throw new HibernateException(e);
    }
    LOGGER.debug("Agroal Configured");
}
Also used : ConnectionProviderInitiator(org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator) Stoppable(org.hibernate.service.spi.Stoppable) Connection(java.sql.Connection) AvailableSettings(org.hibernate.cfg.AvailableSettings) Configurable(org.hibernate.service.spi.Configurable) Logger(org.jboss.logging.Logger) UnknownUnwrapTypeException(org.hibernate.service.UnknownUnwrapTypeException) AgroalPropertiesReader(io.agroal.api.configuration.supplier.AgroalPropertiesReader) NamePrincipal(io.agroal.api.security.NamePrincipal) Function(java.util.function.Function) AgroalConnectionFactoryConfigurationSupplier(io.agroal.api.configuration.supplier.AgroalConnectionFactoryConfigurationSupplier) Consumer(java.util.function.Consumer) SQLException(java.sql.SQLException) Map(java.util.Map) DataSource(javax.sql.DataSource) AgroalConnectionFactoryConfiguration(io.agroal.api.configuration.AgroalConnectionFactoryConfiguration) SimplePassword(io.agroal.api.security.SimplePassword) HibernateException(org.hibernate.HibernateException) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) AgroalDataSource(io.agroal.api.AgroalDataSource) HibernateException(org.hibernate.HibernateException) AgroalPropertiesReader(io.agroal.api.configuration.supplier.AgroalPropertiesReader) UnknownUnwrapTypeException(org.hibernate.service.UnknownUnwrapTypeException) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException)

Example 2 with AgroalPropertiesReader

use of io.agroal.api.configuration.supplier.AgroalPropertiesReader 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)2 AgroalPropertiesReader (io.agroal.api.configuration.supplier.AgroalPropertiesReader)2 SQLException (java.sql.SQLException)2 AgroalConnectionFactoryConfiguration (io.agroal.api.configuration.AgroalConnectionFactoryConfiguration)1 AgroalDataSourceConfiguration (io.agroal.api.configuration.AgroalDataSourceConfiguration)1 AgroalConnectionFactoryConfigurationSupplier (io.agroal.api.configuration.supplier.AgroalConnectionFactoryConfigurationSupplier)1 NamePrincipal (io.agroal.api.security.NamePrincipal)1 SimplePassword (io.agroal.api.security.SimplePassword)1 Connection (java.sql.Connection)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 DataSource (javax.sql.DataSource)1 IndyLifecycleException (org.commonjava.indy.action.IndyLifecycleException)1 HibernateException (org.hibernate.HibernateException)1 AvailableSettings (org.hibernate.cfg.AvailableSettings)1 ConnectionProviderInitiator (org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator)1