Skip to content

Commit 0b5588a

Browse files
committed
Enhance jdbcSuspendTransaction to support transaction reuse for improved performance
1 parent e582f0f commit 0b5588a

File tree

1 file changed

+10
-1
lines changed
  • krescent-exposed/src/main/kotlin/dev/helight/krescent/exposed

1 file changed

+10
-1
lines changed

krescent-exposed/src/main/kotlin/dev/helight/krescent/exposed/Utils.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@ import kotlinx.coroutines.coroutineScope
66
import kotlinx.coroutines.runBlocking
77
import org.jetbrains.exposed.sql.Database
88
import org.jetbrains.exposed.sql.Transaction
9+
import org.jetbrains.exposed.sql.transactions.TransactionManager
910
import org.jetbrains.exposed.sql.transactions.transaction
1011

11-
internal suspend fun <T> jdbcSuspendTransaction(database: Database, statement: suspend Transaction.() -> T): T =
12+
internal suspend fun <T> jdbcSuspendTransaction(
13+
database: Database,
14+
reuseTransaction: Boolean = false,
15+
statement: suspend Transaction.() -> T,
16+
): T =
1217
coroutineScope {
18+
if (reuseTransaction) TransactionManager.currentOrNull()
19+
?.takeIf { it.db == database }
20+
?.let { return@coroutineScope statement.invoke(it) }
21+
1322
async(Dispatchers.IO) {
1423
transaction(database) {
1524
runBlocking {

0 commit comments

Comments
 (0)