- All Implemented Interfaces:
Extension
mySQL Example:
application.conf:
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.- Since:
- 2.0.0
- Author:
- edgar
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new Hikari module using thedb
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. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
databaseName
(String url) Get a database name from jdbc url.static String
databaseType
(String url) Get a database type from jdbc url.healthCheckRegistry
(Object healthCheckRegistry) Sets aHealthCheckRegistry
to pass it forward toHikariConfig
for instrumentation.void
metricRegistry
(Object metricRegistry) Sets aMetricRegistry
to pass it forward toHikariConfig
for instrumentation.
-
Field Details
-
KEY
Default datasource key. Used for retrieving the default datasource.
-
-
Constructor Details
-
HikariModule
Creates a new Hikari module. The database parameter can be one of:- A property key defined in your application configuration file, like
db
. - A special h2 database: mem, local or tmp. - A jdbc connection string, like:jdbc:mysql://localhost/db
- Parameters:
database
- Database key, database type or connection string.
-
HikariModule
public HikariModule()Creates a new Hikari module using thedb
property key. This key must be present in the application configuration file, like:db.url = "jdbc:url" db.user = dbuser db.password = dbpass
-
HikariModule
public HikariModule(@NonNull com.zaxxer.hikari.HikariConfig hikari) Creates a new Hikari module using the Hikari configuration.- Parameters:
hikari
- Hikari configuration.
-
-
Method Details
-
metricRegistry
Sets aMetricRegistry
to pass it forward toHikariConfig
for instrumentation.- Parameters:
metricRegistry
- an instance compatible withHikariConfig.setMetricRegistry(Object)
- Returns:
- this instance
- See Also:
-
HikariConfig.setMetricRegistry(Object)
-
healthCheckRegistry
Sets aHealthCheckRegistry
to pass it forward toHikariConfig
for instrumentation.- Parameters:
healthCheckRegistry
- an instance compatible withHikariConfig.setHealthCheckRegistry(Object)
- Returns:
- this instance
- See Also:
-
HikariConfig.setHealthCheckRegistry(Object)
-
install
-
databaseType
Get a database type from jdbc url. Examples:- jdbc:mysql://localhost/mydb => mysql - jdbc:postgresql://server/database => postgresql
- Parameters:
url
- Jdbc connection string (a.k.a jdbc url)- Returns:
- Database type or given jdbc connection string for unknown or bad urls.
-
databaseName
Get a database name from jdbc url. Examples:- jdbc:mysql://localhost/mydb => mydb - jdbc:postgresql://server/database => database
- Parameters:
url
- Jdbc connection string (a.k.a jdbc url)- Returns:
- Database name.
-