Package ratpack.hikari
Class HikariModule
- java.lang.Object
-
- com.google.inject.AbstractModule
-
- ratpack.guice.ConfigurableModule<com.zaxxer.hikari.HikariConfig>
-
- ratpack.hikari.HikariModule
-
- All Implemented Interfaces:
com.google.inject.Module
public class HikariModule extends ConfigurableModule<com.zaxxer.hikari.HikariConfig>
An extension module that provides aDataSource
from a HikariCP JDBC connection pool.This is a
ConfigurableModule
, exposing theHikariConfig
type as the configuration. All aspects of the connection pool can be configured through this object. SeeConfigurableModule
for usage patterns.import ratpack.core.service.Service; import ratpack.core.service.StartEvent; import ratpack.exec.Blocking; import ratpack.guice.Guice; import ratpack.hikari.HikariModule; import ratpack.test.embed.EmbeddedApp; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import static org.junit.jupiter.api.Assertions.*; public class Example { static class InitDb implements Service { public void onStart(StartEvent startEvent) throws Exception { DataSource dataSource = startEvent.getRegistry().get(DataSource.class); try (Connection connection = dataSource.getConnection()) { connection.createStatement().executeUpdate("create table if not exists val(ID INT PRIMARY KEY, val VARCHAR(255));"); } } } public static void main(String... args) throws Exception { EmbeddedApp.of(s -> s .registry(Guice.registry(b -> b .module(HikariModule.class, hikariConfig -> { hikariConfig.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); hikariConfig.addDataSourceProperty("URL", "jdbc:h2:mem:dev"); // Use H2 in memory database }) .bind(InitDb.class) )) .handlers(chain -> chain .post("set/:val", ctx -> Blocking.get(() -> { try (Connection connection = ctx.get(DataSource.class).getConnection()) { PreparedStatement statement = connection.prepareStatement("merge into val (id, val) key(id) values (?, ?)"); statement.setInt(1, 1); statement.setString(2, ctx.getPathTokens().get("val")); return statement.executeUpdate(); } }).then(result -> ctx.render(result.toString()) ) ) .get("get", ctx -> Blocking.get(() -> { try (Connection connection = ctx.get(DataSource.class).getConnection()) { PreparedStatement statement = connection.prepareStatement("select val from val where id = ?"); statement.setInt(1, 1); ResultSet resultSet = statement.executeQuery(); resultSet.next(); return resultSet.getString(1); } }).then(ctx::render) ) ) ).test(httpClient -> { httpClient.post("set/foo"); assertEquals("foo", httpClient.getText("get")); }); } }
- See Also:
- HikariCP,
HikariConfig
,ConfigurableModule
-
-
Constructor Summary
Constructors Constructor Description HikariModule()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
configure()
DataSource
dataSource(HikariService service)
protected DataSource
getDataSource(HikariService service)
com.zaxxer.hikari.HikariDataSource
hikariDataSource(com.zaxxer.hikari.HikariConfig config)
com.zaxxer.hikari.pool.HikariPool
hikariPool(com.zaxxer.hikari.HikariDataSource hikariDataSource)
HikariService
hikariService(com.zaxxer.hikari.HikariDataSource hikariDataSource)
-
Methods inherited from class ratpack.guice.ConfigurableModule
configure, createConfig, defaultConfig, setConfig
-
Methods inherited from class com.google.inject.AbstractModule
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBinding
-
-
-
-
Method Detail
-
configure
protected void configure()
- Overrides:
configure
in classcom.google.inject.AbstractModule
-
hikariDataSource
@Provides public com.zaxxer.hikari.HikariDataSource hikariDataSource(com.zaxxer.hikari.HikariConfig config)
-
hikariPool
@Provides public com.zaxxer.hikari.pool.HikariPool hikariPool(com.zaxxer.hikari.HikariDataSource hikariDataSource)
-
hikariService
@Provides public HikariService hikariService(com.zaxxer.hikari.HikariDataSource hikariDataSource)
-
dataSource
@Provides public DataSource dataSource(HikariService service)
-
getDataSource
protected DataSource getDataSource(HikariService service)
-
-