@@ -4,6 +4,7 @@ import com.github.michaelbull.jdbc.context.CoroutineConnection
44import com.github.michaelbull.jdbc.context.CoroutineTransaction
55import com.github.michaelbull.jdbc.context.connection
66import com.github.michaelbull.jdbc.context.transaction
7+ import kotlinx.coroutines.CoroutineScope
78import kotlinx.coroutines.withContext
89import java.sql.Connection
910import kotlin.contracts.InvocationKind
@@ -26,7 +27,7 @@ import kotlin.coroutines.coroutineContext
2627 * the transaction will [rollback][Connection.rollback] and re-throw the [Throwable], otherwise the transaction will
2728 * [commit][Connection.commit] and return the result of type [T].
2829 */
29- suspend inline fun <T > transaction (crossinline block : suspend () -> T ): T {
30+ suspend inline fun <T > transaction (crossinline block : suspend CoroutineScope . () -> T ): T {
3031 contract {
3132 callsInPlace(block, InvocationKind .AT_MOST_ONCE )
3233 }
@@ -39,7 +40,11 @@ suspend inline fun <T> transaction(crossinline block: suspend () -> T): T {
3940 execute(block)
4041 }
4142 }
42- existingTransaction.isRunning -> block()
43+
44+ existingTransaction.isRunning -> withContext(coroutineContext) {
45+ block()
46+ }
47+
4348 else -> error(" Attempted to start new transaction within: $existingTransaction " )
4449 }
4550}
@@ -53,7 +58,7 @@ suspend inline fun <T> transaction(crossinline block: suspend () -> T): T {
5358 * [commit][Connection.commit].
5459 */
5560@PublishedApi
56- internal suspend inline fun <T > execute (crossinline block : suspend () -> T ): T {
61+ internal suspend inline fun <T > execute (crossinline block : suspend CoroutineScope . () -> T ): T {
5762 contract {
5863 callsInPlace(block, InvocationKind .AT_MOST_ONCE )
5964 }
@@ -65,7 +70,7 @@ internal suspend inline fun <T> execute(crossinline block: suspend () -> T): T {
6570 connection.autoCommit = false
6671
6772 try {
68- val result = block()
73+ val result = withContext(coroutineContext) { block() }
6974 transaction.complete()
7075 connection.commit()
7176 return result
0 commit comments