Skip to content

Commit

Permalink
feat: expose the number of times and the delay between each attempt D…
Browse files Browse the repository at this point in the history
…HIS2 will try to connect to the database [DHIS2-16091] (#15485)

* feat: expose database "acquire connection retry attempts" through dhis2.conf

* chore: expose delay between each attempt
  • Loading branch information
tonsV2 authored Nov 3, 2023
1 parent 11ffcfd commit b787c6d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,36 @@ public enum ConfigurationKey {
*/
CONNECTION_POOL_ACQUIRE_INCR("connection.pool.acquire_incr", "5", false),

/**
* Determines how many times the system will try to acquire a connection before giving up. If this
* value is less than or equal to zero, the system will keep trying indefinitely. (default: 30).
*/
CONNECTION_POOL_ACQUIRE_RETRY_ATTEMPTS("connection.pool.acquire_retry_attempts", "30", false),

/**
* Determines the delay in milliseconds, c3p0 will wait between acquire attempts. (default: 1000)
*/
CONNECTION_POOL_ACQUIRE_RETRY_DELAY("connection.pool.acquire_retry_delay", "1", false),

/**
* Determines how many connections at a time will try to acquire when the pool is exhausted.
* (default: 5).
*/
ANALYTICS_CONNECTION_POOL_ACQUIRE_INCR("analytics.connection.pool.acquire_incr", "5", false),

/**
* Determines how many times the system will try to acquire a connection before giving up. If this
* value is less than or equal to zero, the system will keep trying indefinitely. (default: 30).
*/
ANALYTICS_CONNECTION_POOL_ACQUIRE_RETRY_ATTEMPTS(
"analytics.connection.pool.acquire_retry_attempts", "30", false),

/**
* Determines the delay in milliseconds, c3p0 will wait between acquire attempts. (default: 1000)
*/
ANALYTICS_CONNECTION_POOL_ACQUIRE_RETRY_DELAY(
"analytics.connection.pool.acquire_retry_delay", "1", false),

/**
* Seconds a Connection can remain pooled but unused before being discarded. Zero means idle
* connections never expire (default: 7200).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_CONNECTION_DRIVER_CLASS;
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;
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_CONNECTION_POOL_ACQUIRE_RETRY_DELAY;
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_CONNECTION_POOL_IDLE_CON_TEST_PERIOD;
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_CONNECTION_POOL_INITIAL_SIZE;
import static org.hisp.dhis.external.conf.ConfigurationKey.ANALYTICS_CONNECTION_POOL_MAX_IDLE_TIME;
Expand All @@ -50,6 +52,8 @@
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_DRIVER_CLASS;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_PASSWORD;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_ACQUIRE_INCR;
import static org.hisp.dhis.external.conf.ConfigurationKey.CONNECTION_POOL_ACQUIRE_RETRY_ATTEMPTS;
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_MAX_IDLE_TIME;
Expand Down Expand Up @@ -113,6 +117,10 @@ public enum ConfigKeyMapper {
.put(CONNECTION_POOL_VALIDATION_TIMEOUT, ANALYTICS_CONNECTION_POOL_VALIDATION_TIMEOUT)
/* C3P0-specific */
.put(CONNECTION_POOL_ACQUIRE_INCR, ANALYTICS_CONNECTION_POOL_ACQUIRE_INCR)
.put(
CONNECTION_POOL_ACQUIRE_RETRY_ATTEMPTS,
ANALYTICS_CONNECTION_POOL_ACQUIRE_RETRY_ATTEMPTS)
.put(CONNECTION_POOL_ACQUIRE_RETRY_DELAY, ANALYTICS_CONNECTION_POOL_ACQUIRE_RETRY_DELAY)
.put(CONNECTION_POOL_MAX_IDLE_TIME, ANALYTICS_CONNECTION_POOL_MAX_IDLE_TIME)
.put(CONNECTION_POOL_MIN_SIZE, ANALYTICS_CONNECTION_POOL_MIN_SIZE)
.put(CONNECTION_POOL_INITIAL_SIZE, ANALYTICS_CONNECTION_POOL_INITIAL_SIZE)
Expand Down Expand Up @@ -159,6 +167,9 @@ public static class PoolConfig {

private String acquireIncrement;

private String acquireRetryAttempts;
private String acquireRetryDelay;

private String maxIdleTime;

private ConfigKeyMapper mapper;
Expand Down Expand Up @@ -271,6 +282,17 @@ private static DataSource createC3p0DbPool(PoolConfig config)
firstNonNull(
config.getAcquireIncrement(),
dhisConfig.getProperty(mapper.getConfigKey(CONNECTION_POOL_ACQUIRE_INCR))));
final int acquireRetryAttempts =
parseInt(
firstNonNull(
config.getAcquireRetryAttempts(),
dhisConfig.getProperty(
mapper.getConfigKey(CONNECTION_POOL_ACQUIRE_RETRY_ATTEMPTS))));
final int acquireRetryDelay =
parseInt(
firstNonNull(
config.getAcquireRetryDelay(),
dhisConfig.getProperty(mapper.getConfigKey(CONNECTION_POOL_ACQUIRE_RETRY_DELAY))));
final int maxIdleTime =
parseInt(
firstNonNull(
Expand Down Expand Up @@ -304,6 +326,8 @@ private static DataSource createC3p0DbPool(PoolConfig config)
dataSource.setMinPoolSize(minPoolSize);
dataSource.setInitialPoolSize(initialSize);
dataSource.setAcquireIncrement(acquireIncrement);
dataSource.setAcquireRetryAttempts(acquireRetryAttempts);
dataSource.setAcquireRetryDelay(acquireRetryDelay);
dataSource.setMaxIdleTime(maxIdleTime);
dataSource.setTestConnectionOnCheckin(testOnCheckIn);
dataSource.setTestConnectionOnCheckout(testOnCheckOut);
Expand Down

0 comments on commit b787c6d

Please sign in to comment.