== Hikari

https://github.com/brettwooldridge/HikariCP[HikariCP] is a high-performance JDBC connection pool.

=== Usage

1) Add the dependency:

[dependency, artifactId="jooby-hikari"]
.

2) Add database driver (MySQL here):

[dependency, groupId="mysql", artifactId="mysql-connector-java", version="${mysql-connector-java.version}"]
.

3) Set database properties

.application.conf
[source, properties]
----
db.url = "jdbc:mysql://localhost/mydb"
db.user = myuser
db.password = mypass
----


4) Install and use the connection pool

.Java
[source, java, role="primary"]
----
import io.jooby.hikari.HikariModule;

{
  install(new HikariModule());

  get("/", ctx -> {
    DataSource ds = require(DataSource.class);
    // work with data source
  });
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.hikari.HikariModule

{
  install(HikariModule())
  
  get("/") {
    val ds = require(DataSource::class)
    // work with data source
  }
}
----

=== Database configuration

By default the javadoc:hikari.HikariModule[text="Hikari", artifact="jooby-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
[source, java, role="primary"]
----
import io.jooby.hikari.HikariModule;

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

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.hikari.HikariModule

{
  install(HikariModule("mydb"))
}
----

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

.No properties
[source, java, role="primary"]
----
import io.jooby.hikari.HikariModule;

{
  install(new HikariModule("jdbc:mysql://localhost/mydb"));
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.hikari.HikariModule

{
  install(HikariModule("jdbc:mysql://localhost/mydb"))
}
----

=== Special databases

HikariModule comes with 3 special databases provided by https://github.com/h2database/h2database[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

=== Multiple databases

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

.application.conf
[source, properties]
----
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
[source, java, role="primary"]
----
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");
    // ...
  });
}
----

.Kotlin
[source, kt, role="secondary"]
----
import io.jooby.hikari.HikariModule

{
  install(HikariModule("maindb"))
 
  install(HikariModule("auditdb"))

  get("/") {
    val maindb  = require(DataSource::class, "maindb")
    val auditdb = require(DataSource::class, "auditdb")
    // ...
  }
}
----

=== Advanced options

Advanced Hikari configuration is supported from application configuration properties.

.Maximum Pool Size
[source, properties]
----
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
[source, properties]
----
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
----
