Skip to content

Latest commit

 

History

History
91 lines (79 loc) · 2.76 KB

README.md

File metadata and controls

91 lines (79 loc) · 2.76 KB

HikariCP extension for Ktor

Version Java CI with Gradle GitHub License

This module allows to get up HikariCP connection pool at application start.

Quick start

Maven

<repositories>
    <repository>
      <id>exktor</id>
      <url>https://maven.pkg.github.com/paslavsky/exktor</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>net.paslavsky</groupId>
        <artifactId>ktor-sql</artifactId>
        <version>${exktor.version}</version>
    </dependency>
</dependencies>

Gradle

repositories {
  maven {
    url = uri("https://maven.pkg.github.com/paslavsky/exktor")
  }
}

dependencies {
  implementation "net.paslavsky:ktor-sql:$exktorVersion"
}

Connection pool configuration

fun Application.module() {
    install(SqlFeature) { // this: HikariConfig
        // ...
    }
}

Almost all configs could be configured at the configuration file:

  • dataSource.dataSourceClassName
  • dataSource.jdbcUrl
  • dataSource.username
  • dataSource.password
  • dataSource.autoCommit
  • dataSource.connectionTimeout
  • dataSource.idleTimeout
  • dataSource.maxLifetime
  • dataSource.connectionTestQuery
  • dataSource.minimumIdle
  • dataSource.maximumPoolSize
  • dataSource.poolName
  • dataSource.initializationFailTimeout
  • dataSource.isolateInternalQueries
  • dataSource.allowPoolSuspension
  • dataSource.readOnly
  • dataSource.registerMbeans
  • dataSource.catalog
  • dataSource.connectionInitSql
  • dataSource.driverClassName
  • dataSource.transactionIsolation
  • dataSource.validationTimeout
  • dataSource.leakDetectionThreshold
  • dataSource.schema

For more information please read HikaryCP official documentation.

Note: Programmatic configuration will override configs from configuration file

Access to the connection pool

Anywhere inside Application context you could use variable dataSource (javax.sql.DataSource) to access to the connection pool.

Events

ktor-sql module listen to standard ktor events to create/close connection poll and producing own events:

  • ApplicationStarted
    • DBConnecting - before create connection pool
    • DBConnected - when connection pool was successfully created
  • ApplicationStopPreparing
    • DBClosing - before closing connection pool
    • DBClosed - connection poll was closed