Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EXPOSED-133 Suspend transactions blocking Hikari connection pool #1837

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}