public class HikariModule extends AbstractModule
This module provides a DataSource instance that is configured based on module's constructor arguments,
configuration properties or both.
Different constructor variants allow you to configure dataSourceClassName (note that HikariCP uses javax.sql.DataSource instances
instead of java.sql.Driver used by other connection pools), minimumIdle and maximumPoolSize as well as dataSourceProperties.
If you wish to configure the module using configuration properties you should use the following property names: other.hikari.dataSourceClassName,
other.hikari.minimumIdle and other.hikari.maximumPoolSize. All configuration properties prefixed with other.hikari.dataSourceProperties will
be used as data source properties - e.g. other.hikari.URL will be used to set URL property on the data source.
import com.google.common.collect.ImmutableMap;
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;
public class Example {
public static void main(String... args) {
EmbeddedApp.fromHandlerFactory(launchConfig ->
Guice.builder(launchConfig)
.bindings(b ->
// Use H2 in memory database
b.add(new HikariModule(ImmutableMap.of("URL", "jdbc:h2:mem:dev"), "org.h2.jdbcx.JdbcDataSource"))
)
.build(chain -> {
DataSource dataSource = chain.get(DataSource.class);
try (Connection connection = dataSource.getConnection()) {
connection.createStatement().executeUpdate("create table if not exists val(ID INT PRIMARY KEY, val VARCHAR(255));");
}
chain
.post("set/:val", ctx ->
ctx.blocking(() -> {
try (Connection connection = dataSource.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 ->
ctx.blocking(() -> {
try (Connection connection = dataSource.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");
assert httpClient.getText("get").equals("foo");
});
}
}
| Constructor and Description |
|---|
HikariModule() |
HikariModule(Map<String,String> dataSourceProperties,
String dataSourceClassName) |
HikariModule(Map<String,String> dataSourceProperties,
String dataSourceClassName,
Integer minimumIdleSize,
Integer maximumPoolSize) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
configure() |
DataSource |
dataSource(com.zaxxer.hikari.HikariConfig config) |
com.zaxxer.hikari.HikariConfig |
hikariConfig(LaunchConfig launchConfig) |
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBindingpublic HikariModule()
public HikariModule(Map<String,String> dataSourceProperties, String dataSourceClassName)
protected void configure()
configure in class AbstractModule@Provides public com.zaxxer.hikari.HikariConfig hikariConfig(LaunchConfig launchConfig)
@Provides public DataSource dataSource(com.zaxxer.hikari.HikariConfig config)