Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ public enum ConfigurationKey {
*/
ANALYTICS_CONNECTION_POOL_MAX_IDLE_TIME("analytics.connection.pool.max_idle_time", "7200", false),

/** Minimum number of idle connections to maintain, Hikari only (default: 10). */
CONNECTION_POOL_MIN_IDLE("connection.pool.min_idle", "10", false),

/**
* Interval to keep idle connections alive. Does not reset idle timeout, Hikari only (default: 2
* minutes).
*/
CONNECTION_POOL_KEEP_ALIVE_TIME_SECONDS("connection.pool.keep_alive_time_seconds", "120", false),

/**
* Connection max lifetime. An in-use connection will never be retired, only when it is idle will
* it be removed. Hikari only (default: 30 minutes).
*/
CONNECTION_POOL_MAX_LIFETIME_SECONDS("connection.pool.max_lifetime_seconds", "1800", false),

/**
* Number of seconds that Connections in excess of minPoolSize should be permitted to remain idle
* in the pool before being culled (default: 0).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static com.google.common.base.MoreObjects.firstNonNull;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_CONNECTION_PASSWORD;
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_CONNECTION_POOL_ACQUIRE_INCR;
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_CONNECTION_POOL_ACQUIRE_RETRY_ATTEMPTS;
Expand All @@ -57,9 +58,12 @@
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_ACQUIRE_RETRY_DELAY;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_IDLE_CON_TEST_PERIOD;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_INITIAL_SIZE;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_KEEP_ALIVE_TIME_SECONDS;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_MAX_IDLE_TIME;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_MAX_IDLE_TIME_EXCESS_CON;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_MAX_LIFETIME_SECONDS;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_MAX_SIZE;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_MIN_IDLE;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_MIN_SIZE;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_NUM_THREADS;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_TEST_ON_CHECKIN;
Expand Down Expand Up @@ -239,6 +243,14 @@
dhisConfig.getProperty(mapper.getConfigKey(CONNECTION_POOL_MAX_SIZE))));
final String connectionTestQuery =
dhisConfig.getProperty(mapper.getConfigKey(CONNECTION_POOL_TEST_QUERY));
final int minIdleConnections =
parseInt(dhisConfig.getProperty(mapper.getConfigKey(CONNECTION_POOL_MIN_IDLE)));
final int keepAliveTimeSeconds =
parseInt(
dhisConfig.getProperty(mapper.getConfigKey(CONNECTION_POOL_KEEP_ALIVE_TIME_SECONDS)));
final int maxLifetimeSeconds =
parseInt(dhisConfig.getProperty(mapper.getConfigKey(CONNECTION_POOL_MAX_LIFETIME_SECONDS)));
final ConfigurationKey maxIdleTimeKey = mapper.getConfigKey(CONNECTION_POOL_MAX_IDLE_TIME);

HikariConfig hc = new HikariConfig();
hc.setPoolName(config.getDataSourceName());
Expand All @@ -250,6 +262,22 @@
hc.addDataSourceProperty("prepStmtCacheSize", "250");
hc.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
hc.setConnectionTestQuery(connectionTestQuery);

// Configure the pool before constructing the data source: Hikari validates and starts the pool
// in the constructor, so pool sizing set afterwards is applied to an already filled pool.
hc.setConnectionTimeout(connectionTimeout);
hc.setValidationTimeout(validationTimeout);
hc.setMaximumPoolSize(maxPoolSize);
hc.setMinimumIdle(minIdleConnections);
hc.setKeepaliveTime(SECONDS.toMillis(keepAliveTimeSeconds));
hc.setMaxLifetime(SECONDS.toMillis(maxLifetimeSeconds));

// The max idle time default (7200s) is sized for c3p0 and exceeds Hikari's max lifetime, which
// would disable idle eviction. Only apply it to Hikari when configured explicitly.
if (dhisConfig.hasProperty(maxIdleTimeKey)) {
hc.setIdleTimeout(SECONDS.toMillis(parseLong(dhisConfig.getProperty(maxIdleTimeKey))));
}

final String leakThresholdStr =
dhisConfig.getProperty(mapper.getConfigKey(CONNECTION_POOL_WARN_MAX_AGE));

Expand Down Expand Up @@ -280,12 +308,7 @@
hc.setMetricsTrackerFactory(new MicrometerMetricsTrackerFactory(meterRegistry));
}

HikariDataSource ds = new HikariDataSource(hc);
ds.setConnectionTimeout(connectionTimeout);
ds.setValidationTimeout(validationTimeout);
ds.setMaximumPoolSize(maxPoolSize);

return ds;
return new HikariDataSource(hc);
}

/** Create a data source based on a C3p0 connection pool. */
Expand Down Expand Up @@ -340,7 +363,7 @@
final int numHelperThreads =
parseInt(dhisConfig.getProperty(mapper.getConfigKey(CONNECTION_POOL_NUM_THREADS)));

final ComboPooledDataSource pooledDataSource = new ComboPooledDataSource();

Check failure on line 366 in dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/datasource/DatabasePoolUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use try-with-resources or close this "ComboPooledDataSource" in a "finally" clause.

See more on https://sonarcloud.io/project/issues?id=dhis2_dhis2-core&issues=AaEVk9hs3MSjhg4IIgGM&open=AaEVk9hs3MSjhg4IIgGM&pullRequest=25543
pooledDataSource.setDriverClass(driverClassName);
pooledDataSource.setJdbcUrl(jdbcUrl);
pooledDataSource.setUser(username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
*/
package org.hisp.dhis.datasource;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
Expand Down Expand Up @@ -118,6 +121,42 @@ void testCreateDbPoolWhenDbPoolTypeIsC3P0() throws PropertyVetoException, SQLExc

@Test
void testCreateDbPoolWhenDbPoolTypeIsHikari() throws PropertyVetoException, SQLException {
DhisConfigurationProvider mockDhisConfigurationProvider = mockHikariConfiguration();
given(mockDhisConfigurationProvider.hasProperty(ConfigurationKey.CONNECTION_POOL_MAX_IDLE_TIME))
.willReturn(true);
given(mockDhisConfigurationProvider.getProperty(ConfigurationKey.CONNECTION_POOL_MAX_IDLE_TIME))
.willReturn("25");

DataSource dataSource =
DatabasePoolUtils.createDbPool(
hikariPoolConfig(mockDhisConfigurationProvider), meterRegistry);

try (HikariDataSource hikari = assertInstanceOf(HikariDataSource.class, dataSource)) {
assertEquals(5, hikari.getMaximumPoolSize());
assertEquals(3, hikari.getMinimumIdle());
assertEquals(SECONDS.toMillis(60), hikari.getKeepaliveTime());
assertEquals(SECONDS.toMillis(1200), hikari.getMaxLifetime());
assertEquals(SECONDS.toMillis(25), hikari.getIdleTimeout());
}
}

@Test
void testCreateDbPoolWhenDbPoolTypeIsHikariKeepsDefaultIdleTimeoutWhenNotConfigured()
throws PropertyVetoException, SQLException {
DhisConfigurationProvider mockDhisConfigurationProvider = mockHikariConfiguration();
given(mockDhisConfigurationProvider.getProperty(ConfigurationKey.CONNECTION_POOL_MAX_IDLE_TIME))
.willReturn("7200");

DataSource dataSource =
DatabasePoolUtils.createDbPool(
hikariPoolConfig(mockDhisConfigurationProvider), meterRegistry);

try (HikariDataSource hikari = assertInstanceOf(HikariDataSource.class, dataSource)) {
assertEquals(new HikariConfig().getIdleTimeout(), hikari.getIdleTimeout());
}
}

private static DhisConfigurationProvider mockHikariConfiguration() {
DhisConfigurationProvider mockDhisConfigurationProvider = mock(DhisConfigurationProvider.class);
given(mockDhisConfigurationProvider.getProperty(ConfigurationKey.CONNECTION_DRIVER_CLASS))
.willReturn("org.hisp.dhis.datasource.StubDriver");
Expand All @@ -127,20 +166,27 @@ void testCreateDbPoolWhenDbPoolTypeIsHikari() throws PropertyVetoException, SQLE
mockDhisConfigurationProvider.getProperty(
ConfigurationKey.CONNECTION_POOL_VALIDATION_TIMEOUT))
.willReturn("250");
given(mockDhisConfigurationProvider.getProperty(ConfigurationKey.CONNECTION_POOL_MIN_IDLE))
.willReturn("3");
given(
mockDhisConfigurationProvider.getProperty(
ConfigurationKey.CONNECTION_POOL_KEEP_ALIVE_TIME_SECONDS))
.willReturn("60");
given(
mockDhisConfigurationProvider.getProperty(
ConfigurationKey.CONNECTION_POOL_MAX_LIFETIME_SECONDS))
.willReturn("1200");
return mockDhisConfigurationProvider;
}

DbPoolConfig.DbPoolConfigBuilder poolConfigBuilder =
DbPoolConfig.builder("test_hikari")
.dbPoolType(DatabasePoolUtils.DbPoolType.HIKARI.name())
.jdbcUrl("jdbc:fake:db")
.username("")
.password("")
.maxPoolSize("1")
.acquireIncrement("1")
.maxIdleTime(String.valueOf(ThreadLocalRandom.current().nextInt()))
.dhisConfig(mockDhisConfigurationProvider);

DataSource dataSource =
DatabasePoolUtils.createDbPool(poolConfigBuilder.build(), meterRegistry);
assertInstanceOf(HikariDataSource.class, dataSource);
private static DbPoolConfig hikariPoolConfig(DhisConfigurationProvider dhisConfig) {
return DbPoolConfig.builder("test_hikari")
.dbPoolType(DatabasePoolUtils.DbPoolType.HIKARI.name())
.jdbcUrl("jdbc:fake:db")
.username("")
.password("")
.maxPoolSize("5")
.dhisConfig(dhisConfig)
.build();
}
}
Loading