Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HikariCP config fix #1035

Merged
merged 9 commits into from
Apr 24, 2024
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
}
}
Loading