Skip to content

Commit 29397fd

Browse files
committed
Bind PostgresKVStore as default, add HikariCp connection pooling default config
1 parent ee9deeb commit 29397fd

3 files changed

Lines changed: 91 additions & 0 deletions

File tree

app/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ buildscript {
44
ext.protobufVersion = '3.21.7'
55
ext.jerseyVersion = '3.1.0'
66
ext.junitVersion = '5.9.0'
7+
ext.mockitoVersion = '5.2.0'
78
ext.postgresVersion = '42.5.1'
89
ext.jooqVersion = '3.17.7'
910
ext.guiceVersion = '5.1.0'
@@ -42,17 +43,23 @@ dependencies {
4243
implementation "org.jooq:jooq-codegen:$jooqVersion"
4344
runtimeOnly "org.postgresql:postgresql:$postgresVersion"
4445
jooqGenerator "org.postgresql:postgresql:$postgresVersion"
46+
implementation 'com.zaxxer:HikariCP:5.0.1' // Connection pooling for postgres/jdbc
4547

4648
implementation "com.google.inject:guice:$guiceVersion"
4749
implementation "org.glassfish.hk2:guice-bridge:3.0.3"
4850

4951
compileOnly 'org.projectlombok:lombok:1.18.24'
5052
annotationProcessor 'org.projectlombok:lombok:1.18.24'
5153

54+
implementation('org.glassfish.jersey.inject:jersey-hk2:3.1.0')
55+
implementation "org.glassfish.hk2:guice-bridge:3.0.3"
56+
5257
compileOnly 'org.projectlombok:lombok:1.18.24'
5358
annotationProcessor 'org.projectlombok:lombok:1.18.24'
5459

5560
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
61+
testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
62+
testImplementation "org.mockito:mockito-core:$mockitoVersion"
5663
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
5764
testImplementation "org.hamcrest:hamcrest-library:2.2"
5865
testImplementation "org.testcontainers:junit-jupiter:1.17.6"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,74 @@
11
package org.vss.guice;
22

33
import com.google.inject.AbstractModule;
4+
import com.google.inject.Provides;
45
import com.google.inject.Singleton;
6+
import com.zaxxer.hikari.HikariConfig;
7+
import com.zaxxer.hikari.HikariDataSource;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.Properties;
11+
import org.jooq.DSLContext;
12+
import org.jooq.SQLDialect;
13+
import org.jooq.impl.DSL;
514
import org.vss.KVStore;
615
import org.vss.impl.postgres.PostgresBackendImpl;
716

817
public class BaseModule extends AbstractModule {
18+
919
@Override
1020
protected void configure() {
1121
bind(KVStore.class).to(PostgresBackendImpl.class).in(Singleton.class);
1222
}
23+
24+
@Provides
25+
@Singleton
26+
public DSLContext provideDSLContext() throws ClassNotFoundException {
27+
// Required to load postgres drivers in tomcat
28+
Class.forName("org.postgresql.Driver");
29+
return DSL.using(HikariCPDataSource.dataSource, SQLDialect.POSTGRES);
30+
}
31+
}
32+
33+
class HikariCPDataSource {
34+
35+
private static HikariConfig config = new HikariConfig();
36+
public static HikariDataSource dataSource;
37+
38+
static {
39+
try (InputStream input = HikariCPDataSource.class.getClassLoader()
40+
.getResourceAsStream("hikariJdbc.properties")) {
41+
Properties hikariJdbcProperties = new Properties();
42+
hikariJdbcProperties.load(input);
43+
44+
config.setJdbcUrl(hikariJdbcProperties.getProperty("jdbc.url"));
45+
config.setUsername(hikariJdbcProperties.getProperty("jdbc.username"));
46+
config.setPassword(hikariJdbcProperties.getProperty("jdbc.password"));
47+
48+
config.setMaximumPoolSize(
49+
Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.maxPoolSize")));
50+
config.setMinimumIdle(
51+
Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.minimumIdle")));
52+
config.setConnectionTimeout(
53+
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.connectionTimeout")));
54+
config.setIdleTimeout(
55+
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.idleTimeout")));
56+
config.setMaxLifetime(
57+
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.maxLifetime")));
58+
59+
config.addDataSourceProperty("cachePrepStmts",
60+
hikariJdbcProperties.getProperty("hikaricp.cachePrepStmts"));
61+
config.addDataSourceProperty("prepStmtCacheSize",
62+
hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSize"));
63+
config.addDataSourceProperty("prepStmtCacheSqlLimit",
64+
hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSqlLimit"));
65+
66+
dataSource = new HikariDataSource(config);
67+
} catch (IOException e) {
68+
throw new RuntimeException("Unable to read hikariJdbcProperties from resources");
69+
}
70+
}
71+
72+
private HikariCPDataSource() {
73+
}
1374
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Default properties, these are meant to be changed according to application needs.
2+
3+
jdbc.url=jdbc:postgresql://localhost:5432/postgres
4+
jdbc.username=postgres
5+
jdbc.password=
6+
7+
# Idle Timeout
8+
hikaricp.minimumIdle=10
9+
10+
# Set connectionTimeout to 30 secs
11+
hikaricp.connectionTimeout=30000
12+
13+
# Set idle timeout to 10 minutes
14+
hikaricp.idleTimeout=600000
15+
16+
# Set Maximum lifetime of a connection to 30minutes
17+
hikaricp.maxLifetime=1800000
18+
19+
# Performance Optimizations
20+
hikaricp.maxPoolSize=50
21+
hikaricp.cachePrepStmts=true
22+
hikaricp.prepStmtCacheSize=250
23+
hikaricp.prepStmtCacheSqlLimit=2048

0 commit comments

Comments
 (0)