Skip to content

Commit

Permalink
fix: EXPOSED-133 Suspend transactions blocking Hikari connection pool (
Browse files Browse the repository at this point in the history
…JetBrains#1837)

* fix: EXPOSED-133 Suspend transactions blocking Hikari connection pool

If the maximumPoolSize of a HikariDataSource is reached, the active connections
made through newSuspendedTransaction() are not being released back into the pool,
leading to a connection timeout exception when attempting to get more connections.

resetIfClosed(), in suspendedTransactionAsyncInternal(), calls getConnection() to
check if the transaction is closed (may happen after a repetition attempt), so
that it can be properly reset for a new attempt.

This blocks the connections being released and should not be necessary for the
first loop, as the created transaction has an open connection.

resetIfClosed() is now only accessed on subsequent repetition attempt loops.
  • Loading branch information
bog-walk authored and saral committed Oct 3, 2023
1 parent d754149 commit 1379248
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ private fun Transaction.resetIfClosed(): Transaction {
}
}

@Suppress("CyclomaticComplexMethod")
private fun <T> TransactionScope.suspendedTransactionAsyncInternal(
shouldCommit: Boolean,
statement: suspend Transaction.() -> T
Expand All @@ -172,7 +173,7 @@ private fun <T> TransactionScope.suspendedTransactionAsyncInternal(

var answer: T
while (true) {
val transaction = tx.value.resetIfClosed()
val transaction = if (repetitions == 0) tx.value else tx.value.resetIfClosed()

@Suppress("TooGenericExceptionCaught")
try {
Expand Down
1 change: 1 addition & 0 deletions exposed-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation("org.apache.logging.log4j", "log4j-core", Versions.log4j2)
implementation("junit", "junit", "4.12")
implementation("org.hamcrest", "hamcrest-library", "1.3")
implementation("com.zaxxer", "HikariCP", "5.0.1")
implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-debug", Versions.kotlinCoroutines)

implementation("org.testcontainers", "mysql", Versions.testContainers)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.jetbrains.exposed.sql.tests.h2

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.Assume
import org.junit.Test

class ConnectionPoolTests {
private val hikariDataSource1 by lazy {
HikariDataSource(
HikariConfig().apply {
jdbcUrl = "jdbc:h2:mem:hikariDB1"
maximumPoolSize = 10
}
)
}

private val hikariDB1 by lazy {
Database.connect(hikariDataSource1)
}

@Test
fun testSuspendTransactionsExceedingPoolSize() {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
transaction(db = hikariDB1) {
SchemaUtils.create(TestTable)
}

val exceedsPoolSize = (hikariDataSource1.maximumPoolSize * 2 + 1).coerceAtMost(50)
runBlocking {
repeat(exceedsPoolSize) {
launch {
newSuspendedTransaction {
delay(100)
TestEntity.new { testValue = "test$it" }
}
}
}
}

transaction(db = hikariDB1) {
assertEquals(exceedsPoolSize, TestEntity.all().toList().count())

SchemaUtils.drop(TestTable)
}
}

object TestTable : IntIdTable("HIKARI_TESTER") {
val testValue = varchar("test_value", 32)
}

class TestEntity(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<TestEntity>(TestTable)

var testValue by TestTable.testValue
}
}

0 comments on commit 1379248

Please sign in to comment.