|
| 1 | +package org.vss.guice; |
| 2 | + |
| 3 | +import com.google.inject.AbstractModule; |
| 4 | +import com.google.inject.Provides; |
| 5 | +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; |
| 14 | +import org.vss.KVStore; |
| 15 | +import org.vss.impl.postgres.PostgresBackendImpl; |
| 16 | + |
| 17 | +public class BaseModule extends AbstractModule { |
| 18 | + |
| 19 | + @Override |
| 20 | + protected void configure() { |
| 21 | + // Provide PostgresBackend as default implementation for KVStore. |
| 22 | + bind(KVStore.class).to(PostgresBackendImpl.class).in(Singleton.class); |
| 23 | + } |
| 24 | + |
| 25 | + @Provides |
| 26 | + @Singleton |
| 27 | + // Provide DSLContext which is to be used by PostgresBackend |
| 28 | + public DSLContext provideDSLContext() throws ClassNotFoundException { |
| 29 | + // Required to load postgres drivers in tomcat |
| 30 | + Class.forName("org.postgresql.Driver"); |
| 31 | + return DSL.using(HikariCPDataSource.dataSource, SQLDialect.POSTGRES); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// Provide Hikari Connection Pooling configuration for jdbc connection management. |
| 36 | +// Hikari is high-performance connection pooling library which will maintain a set of connections |
| 37 | +// to the database for us. |
| 38 | +// When we provide `HikariCPDataSource` to DSLContext, jOOQ will internally `acquire` and `release` |
| 39 | +// connections from pool. |
| 40 | +// For HikariCP config, we provide some sane defaults, but they are meant to be changed and tuned. |
| 41 | +// For specific parameter functionality, refer to HikariCP docs. |
| 42 | +class HikariCPDataSource { |
| 43 | + |
| 44 | + private static HikariConfig config = new HikariConfig(); |
| 45 | + public static HikariDataSource dataSource; |
| 46 | + |
| 47 | + static { |
| 48 | + try (InputStream input = HikariCPDataSource.class.getClassLoader() |
| 49 | + .getResourceAsStream("hikariJdbc.properties")) { |
| 50 | + Properties hikariJdbcProperties = new Properties(); |
| 51 | + hikariJdbcProperties.load(input); |
| 52 | + |
| 53 | + config.setJdbcUrl(hikariJdbcProperties.getProperty("jdbc.url")); |
| 54 | + config.setUsername(hikariJdbcProperties.getProperty("jdbc.username")); |
| 55 | + config.setPassword(hikariJdbcProperties.getProperty("jdbc.password")); |
| 56 | + |
| 57 | + config.setMaximumPoolSize( |
| 58 | + Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.maxPoolSize"))); |
| 59 | + config.setMinimumIdle( |
| 60 | + Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.minimumIdle"))); |
| 61 | + config.setConnectionTimeout( |
| 62 | + Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.connectionTimeout"))); |
| 63 | + config.setIdleTimeout( |
| 64 | + Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.idleTimeout"))); |
| 65 | + config.setMaxLifetime( |
| 66 | + Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.maxLifetime"))); |
| 67 | + |
| 68 | + config.addDataSourceProperty("cachePrepStmts", |
| 69 | + hikariJdbcProperties.getProperty("hikaricp.cachePrepStmts")); |
| 70 | + config.addDataSourceProperty("prepStmtCacheSize", |
| 71 | + hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSize")); |
| 72 | + config.addDataSourceProperty("prepStmtCacheSqlLimit", |
| 73 | + hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSqlLimit")); |
| 74 | + |
| 75 | + dataSource = new HikariDataSource(config); |
| 76 | + } catch (IOException e) { |
| 77 | + throw new RuntimeException("Unable to read hikariJdbcProperties from resources"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private HikariCPDataSource() { |
| 82 | + } |
| 83 | +} |
0 commit comments