From 5abf9dcc4cf16fb40f606eddc8e90f38507ea9e2 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Fri, 29 Jan 2021 19:31:18 -0600 Subject: [PATCH] Add HikariCP connection pool --- CHANGELOG.md | 1 + build.gradle | 1 + src/main/kotlin/mdnet/MangaDexClient.kt | 14 +++++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf8c699..f26955c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added ### Changed +- [2021-01-29] Add HikariCP connection pool [@carbotaniuman]. ### Deprecated diff --git a/build.gradle b/build.gradle index edba11c..c35a659 100644 --- a/build.gradle +++ b/build.gradle @@ -44,6 +44,7 @@ dependencies { testImplementation group: "org.http4k", name: "http4k-testing-kotest", version: "$http_4k_version" runtimeOnly group: "io.netty", name: "netty-tcnative-boringssl-static", version: "2.0.34.Final" + implementation group: 'com.zaxxer', name: 'HikariCP', version: '4.0.1' implementation group: "com.h2database", name: "h2", version: "1.4.200" implementation "org.ktorm:ktorm-core:$ktorm_version" implementation "org.ktorm:ktorm-jackson:$ktorm_version" diff --git a/src/main/kotlin/mdnet/MangaDexClient.kt b/src/main/kotlin/mdnet/MangaDexClient.kt index 75c16d4..def6a45 100644 --- a/src/main/kotlin/mdnet/MangaDexClient.kt +++ b/src/main/kotlin/mdnet/MangaDexClient.kt @@ -24,6 +24,8 @@ import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException import com.fasterxml.jackson.dataformat.yaml.YAMLFactory import com.fasterxml.jackson.module.kotlin.KotlinModule import com.fasterxml.jackson.module.kotlin.readValue +import com.zaxxer.hikari.HikariConfig +import com.zaxxer.hikari.HikariDataSource import mdnet.Main.dieWithError import mdnet.cache.ImageStorage import mdnet.logging.info @@ -51,6 +53,8 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo private val database: Database private val storage: ImageStorage + private val dataSource: HikariDataSource + private var settings: ClientSettings // state that must only be accessed from the thread on the executor @@ -72,7 +76,14 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo LOGGER.info { "Client settings loaded: $settings" } - database = Database.connect("jdbc:h2:$databaseFile", "org.h2.Driver") + val config = HikariConfig() + config.jdbcUrl = "jdbc:h2:$databaseFile" + config.addDataSourceProperty("cachePrepStmts", "true") + config.addDataSourceProperty("prepStmtCacheSize", "100") + config.addDataSourceProperty("prepStmtCacheSqlLimit", "1000") + dataSource = HikariDataSource(config) + + database = Database.connect(dataSource) storage = ImageStorage( maxSize = (settings.maxCacheSizeInMebibytes * 1024 * 1024 * 0.95).toLong(), /* MiB to bytes */ cacheFolder, @@ -133,6 +144,7 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo } storage.close() + dataSource.close() latch.countDown() }, 0, TimeUnit.SECONDS