Skip to content

Commit

Permalink
Merge pull request #1035 from CDCgov/story/1001/stg-db-connections
Browse files Browse the repository at this point in the history
HikariCP config fix
  • Loading branch information
saquino0827 authored Apr 24, 2024
2 parents fa46bf2 + ccf6fb4 commit e3208f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import gov.hhs.cdc.trustedintermediary.context.ApplicationContext;
import gov.hhs.cdc.trustedintermediary.wrappers.Logger;
import gov.hhs.cdc.trustedintermediary.wrappers.database.ConnectionPool;
import gov.hhs.cdc.trustedintermediary.wrappers.database.DatabaseCredentialsProvider;
import java.sql.Connection;
Expand All @@ -20,6 +21,8 @@ public class HikariConnectionPool implements ConnectionPool {

public final HikariDataSource ds;

private static final Logger LOGGER = ApplicationContext.getImplementation(Logger.class);

private HikariConnectionPool() {
HikariConfig config = constructHikariConfig();
ds = new HikariDataSource(config);
Expand All @@ -41,17 +44,24 @@ static HikariConfig constructHikariConfig() {
String serverName = ApplicationContext.getProperty("DB_URL", "");
String dbName = ApplicationContext.getProperty("DB_NAME", "");
String dbPort = ApplicationContext.getProperty("DB_PORT", "");
String connectionLifetime = ApplicationContext.getProperty("DB_MAX_LIFETIME", "1800000");

HikariConfig config = new HikariDataSource();

try {
String maxLife = ApplicationContext.getProperty("DB_MAX_LIFETIME");
if (!maxLife.isEmpty()) {
config.setMaxLifetime(Long.parseLong(maxLife));
}
} catch (NumberFormatException e) {
LOGGER.logInfo("Using Hikari default DB Max Lifetime");
}

config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
config.addDataSourceProperty("user", user);
config.addDataSourceProperty("password", pass);
config.addDataSourceProperty("serverName", serverName);
config.addDataSourceProperty("databaseName", dbName);
config.addDataSourceProperty("portNumber", dbPort);
config.addDataSourceProperty("maxLifetime", connectionLifetime);

return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HikariConnectionPoolTest extends Specification {
TestApplicationContext.addEnvironmentVariable("DB_NAME", "test_name")
TestApplicationContext.addEnvironmentVariable("DB_PORT", "1234")
TestApplicationContext.addEnvironmentVariable("DB_PASS", "test_pass")
TestApplicationContext.addEnvironmentVariable("DB_MAX_LIFETIME", "9001")

credProviders.getPassword() >> "test_pass"
TestApplicationContext.register(DatabaseCredentialsProvider, credProviders)
Expand All @@ -30,5 +31,34 @@ class HikariConnectionPoolTest extends Specification {
result.getDataSourceProperties().get("serverName") == "test_url"
result.getDataSourceProperties().get("databaseName") == "test_name"
result.getDataSourceProperties().get("portNumber") == "1234"
result.getMaxLifetime() == 9001L
}

def "connection pool works with default DB_MAX_LIFETIME" () {
when:
TestApplicationContext.addEnvironmentVariable("DB_MAX_LIFETIME", "")
def result = HikariConnectionPool.constructHikariConfig()

then:
result.getDataSourceProperties().get("user") == "test_user"
result.getDataSourceProperties().get("password") == "test_pass"
result.getDataSourceProperties().get("serverName") == "test_url"
result.getDataSourceProperties().get("databaseName") == "test_name"
result.getDataSourceProperties().get("portNumber") == "1234"
result.getMaxLifetime() == 1800000L
}

def "connection pool uses default DB_MAX_LIFETIME when a NumberFormatException is thrown" () {
when:
TestApplicationContext.addEnvironmentVariable("DB_MAX_LIFETIME", "kjihugyftrd")
def result = HikariConnectionPool.constructHikariConfig()

then:
result.getDataSourceProperties().get("user") == "test_user"
result.getDataSourceProperties().get("password") == "test_pass"
result.getDataSourceProperties().get("serverName") == "test_url"
result.getDataSourceProperties().get("databaseName") == "test_name"
result.getDataSourceProperties().get("portNumber") == "1234"
result.getMaxLifetime() == 1800000L
}
}

0 comments on commit e3208f9

Please sign in to comment.