Search in sources :

Example 1 with DhisConfigurationProvider

use of org.hisp.dhis.external.conf.DhisConfigurationProvider in project dhis2-core by dhis2.

the class WebTestConfigurationWithJwtTokenAuth method actualDataSource.

@Bean("actualDataSource")
public DataSource actualDataSource(HibernateConfigurationProvider hibernateConfigurationProvider) {
    final DhisConfigurationProvider config = dhisConfigurationProvider();
    String jdbcUrl = config.getProperty(ConfigurationKey.CONNECTION_URL);
    String username = config.getProperty(ConfigurationKey.CONNECTION_USERNAME);
    String dbPoolType = config.getProperty(ConfigurationKey.DB_POOL_TYPE);
    DatabasePoolUtils.PoolConfig.PoolConfigBuilder builder = DatabasePoolUtils.PoolConfig.builder();
    builder.dhisConfig(config);
    builder.hibernateConfig(hibernateConfigurationProvider);
    builder.dbPoolType(dbPoolType);
    try {
        final DataSource dbPool = DatabasePoolUtils.createDbPool(builder.build());
        // H2 JSON functions
        H2SqlFunction.registerH2Functions(dbPool);
        return dbPool;
    } catch (SQLException | PropertyVetoException e) {
        String message = String.format("Connection test failed for main database pool, " + "jdbcUrl: '%s', user: '%s'", jdbcUrl, username);
        log.error(message);
        log.error(DebugUtils.getStackTrace(e));
        throw new IllegalStateException(message, e);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) SQLException(java.sql.SQLException) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) H2DhisConfigurationProvider(org.hisp.dhis.config.H2DhisConfigurationProvider) DataSource(javax.sql.DataSource) Bean(org.springframework.context.annotation.Bean)

Example 2 with DhisConfigurationProvider

use of org.hisp.dhis.external.conf.DhisConfigurationProvider in project dhis2-core by dhis2.

the class DatabasePoolUtils method createC3p0DbPool.

public static DataSource createC3p0DbPool(PoolConfig config) throws PropertyVetoException, SQLException {
    DhisConfigurationProvider dhisConfig = config.getDhisConfig();
    final String driverClassName = dhisConfig.getProperty(ConfigurationKey.CONNECTION_DRIVER_CLASS);
    final String jdbcUrl = MoreObjects.firstNonNull(config.getJdbcUrl(), dhisConfig.getProperty(ConfigurationKey.CONNECTION_URL));
    final String username = MoreObjects.firstNonNull(config.getUsername(), dhisConfig.getProperty(ConfigurationKey.CONNECTION_USERNAME));
    final String password = MoreObjects.firstNonNull(config.getPassword(), dhisConfig.getProperty(ConfigurationKey.CONNECTION_PASSWORD));
    final int maxPoolSize = Integer.parseInt(MoreObjects.firstNonNull(config.getMaxPoolSize(), dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_MAX_SIZE)));
    final int acquireIncrement = Integer.parseInt(MoreObjects.firstNonNull(config.getAcquireIncrement(), dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_ACQUIRE_INCR)));
    final int maxIdleTime = Integer.parseInt(MoreObjects.firstNonNull(config.maxIdleTime, dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_MAX_IDLE_TIME)));
    final int minPoolSize = Integer.parseInt(dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_MIN_SIZE));
    final int initialSize = Integer.parseInt(dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_INITIAL_SIZE));
    boolean testOnCheckIn = dhisConfig.isEnabled(ConfigurationKey.CONNECTION_POOL_TEST_ON_CHECKIN);
    boolean testOnCheckOut = dhisConfig.isEnabled(ConfigurationKey.CONNECTION_POOL_TEST_ON_CHECKOUT);
    final int maxIdleTimeExcessConnections = Integer.parseInt(dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_MAX_IDLE_TIME_EXCESS_CON));
    final int idleConnectionTestPeriod = Integer.parseInt(dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_IDLE_CON_TEST_PERIOD));
    final String preferredTestQuery = dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_TEST_QUERY);
    final int numHelperThreads = Integer.parseInt(dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_NUM_THREADS));
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    dataSource.setDriverClass(driverClassName);
    dataSource.setJdbcUrl(jdbcUrl);
    dataSource.setUser(username);
    dataSource.setPassword(password);
    dataSource.setMaxPoolSize(maxPoolSize);
    dataSource.setMinPoolSize(minPoolSize);
    dataSource.setInitialPoolSize(initialSize);
    dataSource.setAcquireIncrement(acquireIncrement);
    dataSource.setMaxIdleTime(maxIdleTime);
    dataSource.setTestConnectionOnCheckin(testOnCheckIn);
    dataSource.setTestConnectionOnCheckout(testOnCheckOut);
    dataSource.setMaxIdleTimeExcessConnections(maxIdleTimeExcessConnections);
    dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
    dataSource.setPreferredTestQuery(preferredTestQuery);
    dataSource.setNumHelperThreads(numHelperThreads);
    testConnection(dataSource);
    return dataSource;
}
Also used : ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider)

Example 3 with DhisConfigurationProvider

use of org.hisp.dhis.external.conf.DhisConfigurationProvider in project dhis2-core by dhis2.

the class DatabasePoolUtils method createHikariDbPool.

public static DataSource createHikariDbPool(PoolConfig config) throws SQLException {
    DhisConfigurationProvider dhisConfig = config.getDhisConfig();
    final String driverClassName = dhisConfig.getProperty(ConfigurationKey.CONNECTION_DRIVER_CLASS);
    final String jdbcUrl = MoreObjects.firstNonNull(config.getJdbcUrl(), dhisConfig.getProperty(ConfigurationKey.CONNECTION_URL));
    final String username = MoreObjects.firstNonNull(config.getUsername(), dhisConfig.getProperty(ConfigurationKey.CONNECTION_USERNAME));
    final String password = MoreObjects.firstNonNull(config.getPassword(), dhisConfig.getProperty(ConfigurationKey.CONNECTION_PASSWORD));
    final long connectionTimeout = Long.parseLong(dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_TIMEOUT));
    final long validationTimeout = Long.parseLong(dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_VALIDATION_TIMEOUT));
    final int maxPoolSize = Integer.parseInt(MoreObjects.firstNonNull(config.getMaxPoolSize(), dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_MAX_SIZE)));
    final String connectionTestQuery = dhisConfig.getProperty(ConfigurationKey.CONNECTION_POOL_TEST_QUERY);
    HikariConfig hc = new HikariConfig();
    hc.setPoolName("HikariDataSource_" + CodeGenerator.generateCode(10));
    hc.setDriverClassName(driverClassName);
    hc.setJdbcUrl(jdbcUrl);
    hc.setUsername(username);
    hc.setPassword(password);
    hc.addDataSourceProperty("cachePrepStmts", "true");
    hc.addDataSourceProperty("prepStmtCacheSize", "250");
    hc.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    hc.setConnectionTestQuery(connectionTestQuery);
    HikariDataSource ds = new HikariDataSource(hc);
    ds.setConnectionTimeout(connectionTimeout);
    ds.setValidationTimeout(validationTimeout);
    ds.setMaximumPoolSize(maxPoolSize);
    testConnection(ds);
    return ds;
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) HikariConfig(com.zaxxer.hikari.HikariConfig)

Example 4 with DhisConfigurationProvider

use of org.hisp.dhis.external.conf.DhisConfigurationProvider in project dhis2-core by dhis2.

the class WebTestConfiguration method actualDataSource.

@Bean("actualDataSource")
public DataSource actualDataSource(HibernateConfigurationProvider hibernateConfigurationProvider) {
    final DhisConfigurationProvider config = dhisConfigurationProvider();
    String jdbcUrl = config.getProperty(ConfigurationKey.CONNECTION_URL);
    String username = config.getProperty(ConfigurationKey.CONNECTION_USERNAME);
    String dbPoolType = config.getProperty(ConfigurationKey.DB_POOL_TYPE);
    DatabasePoolUtils.PoolConfig.PoolConfigBuilder builder = DatabasePoolUtils.PoolConfig.builder();
    builder.dhisConfig(config);
    builder.hibernateConfig(hibernateConfigurationProvider);
    builder.dbPoolType(dbPoolType);
    try {
        final DataSource dbPool = DatabasePoolUtils.createDbPool(builder.build());
        // H2 JSON functions
        H2SqlFunction.registerH2Functions(dbPool);
        return dbPool;
    } catch (SQLException | PropertyVetoException e) {
        String message = String.format("Connection test failed for main database pool, " + "jdbcUrl: '%s', user: '%s'", jdbcUrl, username);
        log.error(message);
        log.error(DebugUtils.getStackTrace(e));
        throw new IllegalStateException(message, e);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) SQLException(java.sql.SQLException) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) H2DhisConfigurationProvider(org.hisp.dhis.config.H2DhisConfigurationProvider) DataSource(javax.sql.DataSource) Bean(org.springframework.context.annotation.Bean)

Aggregations

DhisConfigurationProvider (org.hisp.dhis.external.conf.DhisConfigurationProvider)4 PropertyVetoException (java.beans.PropertyVetoException)2 SQLException (java.sql.SQLException)2 DataSource (javax.sql.DataSource)2 H2DhisConfigurationProvider (org.hisp.dhis.config.H2DhisConfigurationProvider)2 Bean (org.springframework.context.annotation.Bean)2 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1