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
Add Hikari CP dependency in exposed-tests module.
Add new test suite for Hikari CP.
  • Loading branch information
bog-walk committed Aug 21, 2023
1 parent 0d8fac7 commit 4265bd4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion exposed-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ 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)
implementation("org.testcontainers", "postgresql", Versions.testContainers)
testCompileOnly("org.postgresql", "postgresql", Versions.postgre)
testCompileOnly("com.impossibl.pgjdbc-ng", "pgjdbc-ng", Versions.postgreNG)
compileOnly("com.h2database", "h2", Versions.h2)
implementation("com.h2database", "h2", Versions.h2)
testCompileOnly("org.xerial", "sqlite-jdbc", Versions.sqlLite3)
}

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 4265bd4

Please sign in to comment.