-
Notifications
You must be signed in to change notification settings - Fork 0
/
HikariConnectionPool.java
50 lines (39 loc) · 1.48 KB
/
HikariConnectionPool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.popokis.undertow_vuejs.db;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.Properties;
public final class HikariConnectionPool {
private final DataSource dataSource;
private HikariConnectionPool() {
String env = System.getenv("APP_ENV");
Properties properties = new Properties();
String filename;
if (Objects.isNull(env) || env.equalsIgnoreCase("test")) {
filename = File.separator + "database/db_test_pool.properties";
} else if (env.equalsIgnoreCase("prod") || env.equalsIgnoreCase("production")) {
filename = File.separator + "database/db_prod_pool.properties";
} else {
throw new RuntimeException("Please indicate APP_ENV var: 'prod' or 'test'");
}
try (InputStream fi = HikariConnectionPool.class.getResourceAsStream(filename)) {
properties.load(fi);
} catch (IOException e) {
throw new RuntimeException(filename + " not found. Please create it inside resources folder.");
}
this.dataSource = new HikariDataSource(new HikariConfig(properties));
}
private static class Holder {
private static final HikariConnectionPool INSTANCE = new HikariConnectionPool();
}
public static HikariConnectionPool getInstance() {
return Holder.INSTANCE;
}
public DataSource getDataSource() {
return dataSource;
}
}