forked from JetBrains/Exposed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: EXPOSED-133 Suspend transactions blocking Hikari connection pool (…
…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
Showing
3 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/h2/ConnectionPoolTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |