diff --git a/exposed-tests/build.gradle.kts b/exposed-tests/build.gradle.kts index 8081974f1d..2714933167 100644 --- a/exposed-tests/build.gradle.kts +++ b/exposed-tests/build.gradle.kts @@ -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) } diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/h2/ConnectionPoolTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/h2/ConnectionPoolTests.kt new file mode 100644 index 0000000000..5f68e15bc8 --- /dev/null +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/h2/ConnectionPoolTests.kt @@ -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) : IntEntity(id) { + companion object : IntEntityClass(TestTable) + + var testValue by TestTable.testValue + } +}