From 8876779d1d71292ad827d9e44c4cadad4abac016 Mon Sep 17 00:00:00 2001 From: Gursharan Singh <3442979+G8XSU@users.noreply.github.com> Date: Wed, 12 Jul 2023 01:09:36 -0700 Subject: [PATCH] Bind PostgresKVStore as default, add HikariCp connection pooling default config --- app/build.gradle | 4 ++ .../main/java/org/vss/guice/BaseModule.java | 61 +++++++++++++++++++ app/src/main/resources/hikariJdbc.properties | 23 +++++++ 3 files changed, 88 insertions(+) create mode 100644 app/src/main/resources/hikariJdbc.properties diff --git a/app/build.gradle b/app/build.gradle index 9b91f56..c80ec04 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,6 +43,7 @@ dependencies { implementation "org.jooq:jooq-codegen:$jooqVersion" runtimeOnly "org.postgresql:postgresql:$postgresVersion" jooqGenerator "org.postgresql:postgresql:$postgresVersion" + implementation 'com.zaxxer:HikariCP:5.0.1' // Connection pooling for postgres/jdbc implementation "com.google.inject:guice:$guiceVersion" implementation "org.glassfish.hk2:guice-bridge:3.0.3" @@ -50,6 +51,9 @@ dependencies { compileOnly 'org.projectlombok:lombok:1.18.24' annotationProcessor 'org.projectlombok:lombok:1.18.24' + implementation('org.glassfish.jersey.inject:jersey-hk2:3.1.0') + implementation "org.glassfish.hk2:guice-bridge:3.0.3" + compileOnly 'org.projectlombok:lombok:1.18.24' annotationProcessor 'org.projectlombok:lombok:1.18.24' diff --git a/app/src/main/java/org/vss/guice/BaseModule.java b/app/src/main/java/org/vss/guice/BaseModule.java index 19b9204..44ebc63 100644 --- a/app/src/main/java/org/vss/guice/BaseModule.java +++ b/app/src/main/java/org/vss/guice/BaseModule.java @@ -1,13 +1,74 @@ package org.vss.guice; import com.google.inject.AbstractModule; +import com.google.inject.Provides; import com.google.inject.Singleton; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import org.jooq.DSLContext; +import org.jooq.SQLDialect; +import org.jooq.impl.DSL; import org.vss.KVStore; import org.vss.impl.postgres.PostgresBackendImpl; public class BaseModule extends AbstractModule { + @Override protected void configure() { bind(KVStore.class).to(PostgresBackendImpl.class).in(Singleton.class); } + + @Provides + @Singleton + public DSLContext provideDSLContext() throws ClassNotFoundException { + // Required to load postgres drivers in tomcat + Class.forName("org.postgresql.Driver"); + return DSL.using(HikariCPDataSource.dataSource, SQLDialect.POSTGRES); + } +} + +class HikariCPDataSource { + + private static HikariConfig config = new HikariConfig(); + public static HikariDataSource dataSource; + + static { + try (InputStream input = HikariCPDataSource.class.getClassLoader() + .getResourceAsStream("hikariJdbc.properties")) { + Properties hikariJdbcProperties = new Properties(); + hikariJdbcProperties.load(input); + + config.setJdbcUrl(hikariJdbcProperties.getProperty("jdbc.url")); + config.setUsername(hikariJdbcProperties.getProperty("jdbc.username")); + config.setPassword(hikariJdbcProperties.getProperty("jdbc.password")); + + config.setMaximumPoolSize( + Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.maxPoolSize"))); + config.setMinimumIdle( + Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.minimumIdle"))); + config.setConnectionTimeout( + Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.connectionTimeout"))); + config.setIdleTimeout( + Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.idleTimeout"))); + config.setMaxLifetime( + Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.maxLifetime"))); + + config.addDataSourceProperty("cachePrepStmts", + hikariJdbcProperties.getProperty("hikaricp.cachePrepStmts")); + config.addDataSourceProperty("prepStmtCacheSize", + hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSize")); + config.addDataSourceProperty("prepStmtCacheSqlLimit", + hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSqlLimit")); + + dataSource = new HikariDataSource(config); + } catch (IOException e) { + throw new RuntimeException("Unable to read hikariJdbcProperties from resources"); + } + } + + private HikariCPDataSource() { + } } diff --git a/app/src/main/resources/hikariJdbc.properties b/app/src/main/resources/hikariJdbc.properties new file mode 100644 index 0000000..fd8dcfb --- /dev/null +++ b/app/src/main/resources/hikariJdbc.properties @@ -0,0 +1,23 @@ +# Default properties, these are meant to be changed according to application needs. + +jdbc.url=jdbc:postgresql://localhost:5432/postgres +jdbc.username=postgres +jdbc.password= + +# Idle Timeout +hikaricp.minimumIdle=10 + +# Set connectionTimeout to 30 secs +hikaricp.connectionTimeout=30000 + +# Set idle timeout to 10 minutes +hikaricp.idleTimeout=600000 + +# Set Maximum lifetime of a connection to 30minutes +hikaricp.maxLifetime=1800000 + +# Performance Optimizations +hikaricp.maxPoolSize=50 +hikaricp.cachePrepStmts=true +hikaricp.prepStmtCacheSize=250 +hikaricp.prepStmtCacheSqlLimit=2048