public class HikariModule extends Object implements Extension
db.url = "jdbc:mysql://localhost/mydb"
db.user = myuser
db.password = mypassword
App.java:
{
install(new HikariModule());
}
To simplify development Jooby offers 3 special databases based on H2 Java database engine:
- mem: for in-memory database
- local: for a file system database stored in the current project directory
- tmp: for a file system database stored in the operating system temporary directory
To use any of these database you first need to add the h2 driver to your project and then:
- define the db property in your application configuration file, like
db=mem or
- pass the database type to the HikariModule. new HikariModule("mem")
Alternative you can specify a jdbc connection string:
install(new HikariModule("jdbc:mysql://localhost/mydb"));
The module exposes a DataSource instance which can be retrieve it after installing:
install(new HikariModule("jdbc:mysql://localhost/mydb"));
DataSource dataSource = require(DataSource.class);
Supports multiple database connections:
install(new HikariModule("maindb"));
install(new HikariModule("auditdb"));
DataSource maindb = require(DataSource.class, "maindb");
DataSource auditdb = require(DataSource.class, "auditdb");
Complete documentation is available at: https://jooby.io/modules/hikari.| Modifier and Type | Field and Description |
|---|---|
static ServiceKey<DataSource> |
KEY
Default datasource key.
|
| Constructor and Description |
|---|
HikariModule()
Creates a new Hikari module using the
db property key. |
HikariModule(com.zaxxer.hikari.HikariConfig hikari)
Creates a new Hikari module using the Hikari configuration.
|
HikariModule(String database)
Creates a new Hikari module.
|
| Modifier and Type | Method and Description |
|---|---|
static String |
databaseName(String url)
Get a database name from jdbc url.
|
static String |
databaseType(String url)
Get a database type from jdbc url.
|
HikariModule |
healthCheckRegistry(Object healthCheckRegistry)
Sets a
HealthCheckRegistry to pass it forward to
HikariConfig for instrumentation. |
void |
install(Jooby application) |
HikariModule |
metricRegistry(Object metricRegistry)
Sets a
MetricRegistry to pass it forward to
HikariConfig for instrumentation. |
public static final ServiceKey<DataSource> KEY
public HikariModule(@Nonnull String database)
db.
- A special h2 database: mem, local or tmp.
- A jdbc connection string, like: jdbc:mysql://localhost/dbdatabase - Database key, database type or connection string.public HikariModule()
db property key. This key must be
present in the application configuration file, like:
db.url = "jdbc:url"
db.user = dbuser
db.password = dbpass
public HikariModule(@Nonnull com.zaxxer.hikari.HikariConfig hikari)
hikari - Hikari configuration.public HikariModule metricRegistry(Object metricRegistry)
MetricRegistry to pass it forward to
HikariConfig for instrumentation.metricRegistry - an instance compatible with HikariConfig.setMetricRegistry(Object)HikariConfig.setMetricRegistry(Object)public HikariModule healthCheckRegistry(Object healthCheckRegistry)
HealthCheckRegistry to pass it forward to
HikariConfig for instrumentation.healthCheckRegistry - an instance compatible with HikariConfig.setHealthCheckRegistry(Object)HikariConfig.setHealthCheckRegistry(Object)@Nonnull public static String databaseType(@Nonnull String url)
url - Jdbc connection string (a.k.a jdbc url)Copyright © 2022. All rights reserved.