1. Hikari
HikariCP is a high-performance JDBC connection pool.
1.1. Usage
1) Add the dependency:
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-hikari</artifactId>
<version>3.5.3</version>
</dependency>
2) Add database driver (MySQL here):
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
3) Set database properties
db.url = "jdbc:mysql://localhost/mydb"
db.user = myuser
db.password = mypass
4) Install and use the connection pool
import io.jooby.hikari.HikariModule;
{
install(new HikariModule());
get("/", ctx -> {
DataSource ds = require(DataSource.class);
// work with data source
});
}
1.2. Database configuration
By default the Hikari module expects to find database properties as application
configuration properties under the db
key prefix. The next example shows how to overrides the
database prefix:
import io.jooby.hikari.HikariModule;
{
install(new HikariModule("mydb"));
}
Configuration properties are optionally if the database driver support them from jdbc url:
import io.jooby.hikari.HikariModule;
{
install(new HikariModule("jdbc:mysql://localhost/mydb"));
}
1.3. Special databases
HikariModule comes with 3 special databases provided by H2 database:
-
mem
: setup an in-memory database -
local
: a file system database atSystem.getProperty("user.dir")
-
tmp
: a tmp file system database atSystem.getProperty("user.dir") + "/tmp"
directory
1.4. Multiple databases
Multiple database is supported using jdbc url or database configuration keys.
maindb.url = "jdbc:mysql://localhost/main"
maindb.user = myuser
maindb.password = mypass
auditdb.url = "jdbc:mysql://localhost/audit"
auditdb.user = myuser
auditdb.password = mypass
import io.jooby.hikari.HikariModule;
{
install(new HikariModule("maindb"));
install(new HikariModule("auditdb"));
get("/", ctx -> {
DataSource maindb = require(DataSource.class, "maindb");
DataSource auditdb = require(DataSource.class, "auditdb");
// ...
});
}
1.5. Advanced options
Advanced Hikari configuration is supported from application configuration properties.
db.url = "jdbc:mysql://localhost/main"
db.user = myuser
hikari.maximumPoolSize = 10
Different configuration per database is available by adding the database key:
maindb.url = "jdbc:mysql://localhost/main"
maindb.user = myuser
maindb.password = mypass
maindb.hikari.maximumPoolSize = 10
auditdb.url = "jdbc:mysql://localhost/audit"
auditdb.user = myuser
auditdb.password = mypass
auditdb.hikari.maximumPoolSize = 15