brettwooldridge / HikariCP

光 HikariCP・A solid, high-performance, JDBC connection pool at last.
Apache License 2.0
19.87k stars 2.92k forks source link

Single line of code takes down all connections #2100

Open holasoyender opened 1 year ago

holasoyender commented 1 year ago

I dont really know if this is a bug in Hikari, a bug with JetBrains Exposed library or with my code, but after some research i've tracked down my database connection issues to this single line of code (Kotlin)

val db_name = ((database.connector() as JdbcConnectionImpl).connection as HikariProxyConnection).metaData.databaseProductName

(database being a org.jetbrains.exposed.sql.Database and JdbcConnectionImpl being a org.jetbrains.exposed.sql.statements.jdbc.JdbcConnectionImpl)

This single line doesnt shut down the pooled connections to the database, but it makes all future querys time out indefinitely. This is my Hikari config:


private const val DRIVER_NAME = "org.postgresql.Driver"
private const val ISOLATION_LEVEL = "TRANSACTION_REPEATABLE_READ"

 private fun hikari_pg(): HikariDataSource {

        val config = HikariConfig().apply {
            driverClassName = DRIVER_NAME
            jdbcUrl = "jdbc:${Env.DATABASE_URL}"
            username = Env.DATABASE_USER
            password = Env.DATABASE_PASSWORD
            addDataSourceProperty("ApplicationName", "appname")
            maximumPoolSize = 50
            connectionTimeout = 3000
            idleTimeout = 600000
            transactionIsolation = ISOLATION_LEVEL
        }

        config.validate()
        return HikariDataSource(config)
    }

The time-out error i get is this one:

Transaction attempt #0 failed: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 3000ms.. Statement(s): <statement...>
org.jetbrains.exposed.exceptions.ExposedSQLException: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 3000ms.
    at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:49)
    at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:158)
    at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:144)
    at org.jetbrains.exposed.sql.AbstractQuery.iterator(AbstractQuery.kt:61)
    at kotlin.collections.CollectionsKt___CollectionsKt.toCollection(_Collections.kt:1295)
    at kotlin.collections.CollectionsKt___CollectionsKt.toMutableList(_Collections.kt:1328)
    at kotlin.collections.CollectionsKt___CollectionsKt.toList(_Collections.kt:1319)
    ...
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction$run(ThreadLocalTransactionManager.kt:232)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.access$inTopLevelTransaction$run(ThreadLocalTransactionManager.kt:1)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:278)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:286)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:277)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:200)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:286)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:170)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:157)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:156)
    ...
    at kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)
Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 3000ms.
    at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:696)
    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:181)
    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:146)
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:100)
    at org.jetbrains.exposed.sql.Database$Companion$connect$3.invoke(Database.kt:141)
    at org.jetbrains.exposed.sql.Database$Companion$connect$3.invoke(Database.kt:138)
    at org.jetbrains.exposed.sql.Database$Companion$doConnect$3.invoke(Database.kt:126)
    at org.jetbrains.exposed.sql.Database$Companion$doConnect$3.invoke(Database.kt:127)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager$ThreadLocalTransaction$connectionLazy$1.invoke(ThreadLocalTransactionManager.kt:89)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager$ThreadLocalTransaction$connectionLazy$1.invoke(ThreadLocalTransactionManager.kt:88)
    at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager$ThreadLocalTransaction.getConnection(ThreadLocalTransactionManager.kt:102)
    at org.jetbrains.exposed.sql.Transaction.getConnection(Transaction.kt)
    at org.jetbrains.exposed.sql.statements.Statement.prepared(Statement.kt:24)
    at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:47)
    ... 21 common frames omitted

But the connections are still active and available:

image

Some info about my current setup:

Database: Postgres ver. 15.2 (Debian 15.2-1.pgdg110+1)
Hikari: 5.0.1
PSQL Driver: org.postgresql 42.6.0
kaustubhDsarathi commented 9 months ago

Hi, @holasoyender were you able to find the root cause of the issue?