1. Hikari

HikariCP is a high-performance JDBC connection pool.

1.1. Usage

1) Add the dependency:

Maven
Gradle
<dependency>
  <groupId>io.jooby</groupId>
  <artifactId>jooby-hikari</artifactId>
  <version>3.4.3</version>
</dependency>

2) Add database driver (MySQL here):

Maven
Gradle
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>${mysql-connector-java.version}</version>
</dependency>

3) Set database properties

application.conf
db.url = "jdbc:mysql://localhost/mydb"
db.user = myuser
db.password = mypass

4) Install and use the connection pool

Java
Kotlin
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:

Custom database prefix
Java
Kotlin
import io.jooby.hikari.HikariModule;

{
  install(new HikariModule("mydb"));
}

Configuration properties are optionally if the database driver support them from jdbc url:

No properties
Java
Kotlin
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 at System.getProperty("user.dir")

  • tmp : a tmp file system database at System.getProperty("user.dir") + "/tmp" directory

1.4. Multiple databases

Multiple database is supported using jdbc url or database configuration keys.

application.conf
maindb.url = "jdbc:mysql://localhost/main"
maindb.user = myuser
maindb.password = mypass

auditdb.url = "jdbc:mysql://localhost/audit"
auditdb.user = myuser
auditdb.password = mypass
Multiple data sources
Java
Kotlin
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.

Maximum Pool Size
db.url = "jdbc:mysql://localhost/main"
db.user = myuser

hikari.maximumPoolSize = 10

Different configuration per database is available by adding the database key:

Maximum Pool Size
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