Skip to content

Commit e123b7f

Browse files
committed
Read application properties from env first.
Prioritize environment variables over configuration file properties. This allows the service to adapt dynamically to different deployment environments by overriding config file settings with environment-specific values when available.
1 parent 424a385 commit e123b7f

File tree

3 files changed

+51
-38
lines changed

3 files changed

+51
-38
lines changed

app/src/main/java/org/vss/guice/BaseModule.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,48 @@ class HikariCPDataSource {
5252

5353
static {
5454
try (InputStream input = HikariCPDataSource.class.getClassLoader()
55-
.getResourceAsStream("hikariJdbc.properties")) {
56-
Properties hikariJdbcProperties = new Properties();
57-
hikariJdbcProperties.load(input);
55+
.getResourceAsStream("application.properties")) {
56+
Properties applicationProperties = new Properties();
57+
applicationProperties.load(input);
5858

59-
config.setJdbcUrl(hikariJdbcProperties.getProperty("jdbc.url"));
60-
config.setUsername(hikariJdbcProperties.getProperty("jdbc.username"));
61-
config.setPassword(hikariJdbcProperties.getProperty("jdbc.password"));
59+
config.setJdbcUrl(getEnvOrConfigProperty("vss.jdbc.url", applicationProperties));
60+
config.setUsername(getEnvOrConfigProperty("vss.jdbc.username", applicationProperties));
61+
config.setPassword(getEnvOrConfigProperty("vss.jdbc.password", applicationProperties));
6262

6363
config.setMaximumPoolSize(
64-
Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.maxPoolSize")));
64+
Integer.parseInt(getEnvOrConfigProperty("vss.hikaricp.maxPoolSize", applicationProperties)));
6565
config.setMinimumIdle(
66-
Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.minimumIdle")));
66+
Integer.parseInt(getEnvOrConfigProperty("vss.hikaricp.minimumIdle", applicationProperties)));
6767
config.setConnectionTimeout(
68-
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.connectionTimeout")));
68+
Long.parseLong(getEnvOrConfigProperty("vss.hikaricp.connectionTimeout", applicationProperties)));
6969
config.setIdleTimeout(
70-
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.idleTimeout")));
70+
Long.parseLong(getEnvOrConfigProperty("vss.hikaricp.idleTimeout", applicationProperties)));
7171
config.setMaxLifetime(
72-
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.maxLifetime")));
72+
Long.parseLong(getEnvOrConfigProperty("vss.hikaricp.maxLifetime", applicationProperties)));
7373

7474
config.addDataSourceProperty("cachePrepStmts",
75-
hikariJdbcProperties.getProperty("hikaricp.cachePrepStmts"));
75+
getEnvOrConfigProperty("vss.hikaricp.cachePrepStmts", applicationProperties));
7676
config.addDataSourceProperty("prepStmtCacheSize",
77-
hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSize"));
77+
getEnvOrConfigProperty("vss.hikaricp.prepStmtCacheSize", applicationProperties));
7878
config.addDataSourceProperty("prepStmtCacheSqlLimit",
79-
hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSqlLimit"));
79+
getEnvOrConfigProperty("vss.hikaricp.prepStmtCacheSqlLimit", applicationProperties));
8080

8181
dataSource = new HikariDataSource(config);
8282
} catch (IOException e) {
83-
throw new RuntimeException("Unable to read hikariJdbcProperties from resources");
83+
throw new RuntimeException("Unable to read application.properties from resources");
8484
}
8585
}
8686

87+
// Retrieves the value of a specified property, first checking environment variables,
88+
// then falling back to provided configuration properties if the environment variable is not set.
89+
private static String getEnvOrConfigProperty(String key, Properties hikariJdbcProperties) {
90+
String propertyValue = System.getenv(key);
91+
if (StringUtils.isBlank(propertyValue)) {
92+
propertyValue = hikariJdbcProperties.getProperty(key);
93+
}
94+
return propertyValue;
95+
}
96+
8797
private HikariCPDataSource() {
8898
}
8999
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Application Properties File
2+
# Contains default application properties, these are meant to be changed according to application needs.
3+
# Each property can be overridden by setting an environment variable with the same name.
4+
# For example, to override 'vss.jdbc.url', set an environment variable 'vss.jdbc.url' with the new value.
5+
6+
vss.jdbc.url=jdbc:postgresql://localhost:5432/postgres
7+
vss.jdbc.username=postgres
8+
vss.jdbc.password=YOU_MUST_CHANGE_THIS_PASSWORD
9+
10+
# Idle Timeout
11+
vss.hikaricp.minimumIdle=10
12+
13+
# Set connectionTimeout to 30 secs
14+
vss.hikaricp.connectionTimeout=30000
15+
16+
# Set idle timeout to 10 minutes
17+
vss.hikaricp.idleTimeout=600000
18+
19+
# Set Maximum lifetime of a connection to 30minutes
20+
vss.hikaricp.maxLifetime=1800000
21+
22+
# Performance Optimizations
23+
vss.hikaricp.maxPoolSize=50
24+
vss.hikaricp.cachePrepStmts=true
25+
vss.hikaricp.prepStmtCacheSize=250
26+
vss.hikaricp.prepStmtCacheSqlLimit=2048

app/src/main/resources/hikariJdbc.properties

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)