Skip to content

Commit

Permalink
Implement TransactionProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
IRus committed Jan 17, 2025
1 parent 08d549e commit f640182
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ open class JdbcModule(
)
}

open val transactionProvider: TransactionProvider by lazy {
TransactionProvider(
dslContext = dslContext,
)
}

open val hikariConfig by lazy {
HikariConfig().apply {
poolName = "kotbot-hikari-pool"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ internal data class JooqTransactionContext(
internal val dslContext: DSLContext,
) : TransactionContext

data object MockTransactionContext : TransactionContext

suspend fun <T> TransactionContext.useTx(
block: suspend DSLContext.() -> T,
): T {
return when (val context = this) {
is JooqTransactionContext -> block(context.dslContext)
MockTransactionContext -> error("useTx shouldn't be called with mock transaction")
}
}

val DSLContext.dslContext: DSLContext
inline val DSLContext.dslContext: DSLContext
get() = this
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.heapy.kotbot.infra.jdbc

import io.heapy.kotbot.infra.Loom
import kotlinx.coroutines.*
import org.jooq.DSLContext

class TransactionProvider(
private val dslContext: DSLContext,
) {
suspend fun <R> transaction(
body: suspend TransactionContext.() -> R,
): R {
return withContext(Dispatchers.Loom) {
dslContext.transactionResult { configuration ->
runBlocking {
JooqTransactionContext(configuration.dsl()).body()
}
}
}
}
}

0 comments on commit f640182

Please sign in to comment.