Skip to content

Commit

Permalink
Using coroutines with multiple databases #624
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Aug 16, 2019
1 parent 897790c commit 12d970b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ private suspend fun <T> withTransactionScope(context: CoroutineContext?,
currentTransaction: Transaction?,
db: Database? = null,
body: suspend TransactionScope.() -> T) : T {
return coroutineContext[TransactionScope]?.body() ?: run {
val currentScope = coroutineContext[TransactionScope]
suspend fun newScope() : T {
val manager = (currentTransaction?.db ?: db)?.transactionManager ?: TransactionManager.manager

val tx = currentTransaction ?: manager.newTransaction(manager.defaultIsolationLevel)
Expand All @@ -124,7 +125,13 @@ private suspend fun <T> withTransactionScope(context: CoroutineContext?,

val newContext = context ?: coroutineContext

TransactionScope(tx, newContext + element).body()
return TransactionScope(tx, newContext + element).body()
}
return when {
currentScope == null -> newScope()
currentTransaction != null && currentScope.tx != currentTransaction -> newScope()
db != null && currentScope.tx.db != db -> newScope()
else -> currentScope.body()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.jetbrains.exposed.sql.tests.h2

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.shared.DMLTestsData
import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.experimental.suspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.transactions.transactionManager
import org.junit.After
Expand Down Expand Up @@ -150,4 +154,48 @@ class MultiDatabaseTest {
SchemaUtils.drop(DMLTestsData.Cities)
}
}

@Test
fun testCoroutinesWithMultiDb() = runBlocking {
newSuspendedTransaction(Dispatchers.IO, db1) {
val tr1 = this
SchemaUtils.create(DMLTestsData.Cities)
assertTrue(DMLTestsData.Cities.selectAll().empty())
DMLTestsData.Cities.insert {
it[DMLTestsData.Cities.name] = "city1"
}

newSuspendedTransaction(Dispatchers.IO, db2) {
assertFalse(DMLTestsData.Cities.exists())
SchemaUtils.create(DMLTestsData.Cities)
DMLTestsData.Cities.insert {
it[DMLTestsData.Cities.name] = "city2"
}
DMLTestsData.Cities.insert {
it[DMLTestsData.Cities.name] = "city3"
}
assertEquals(2, DMLTestsData.Cities.selectAll().count())
assertEquals("city3", DMLTestsData.Cities.selectAll().last()[DMLTestsData.Cities.name])

tr1.suspendedTransaction {
assertEquals(1, DMLTestsData.Cities.selectAll().count())
DMLTestsData.Cities.insert {
it[DMLTestsData.Cities.name] = "city4"
}
DMLTestsData.Cities.insert {
it[DMLTestsData.Cities.name] = "city5"
}
assertEquals(3, DMLTestsData.Cities.selectAll().count())
}

assertEquals(2, DMLTestsData.Cities.selectAll().count())
assertEquals("city3", DMLTestsData.Cities.selectAll().last()[DMLTestsData.Cities.name])
SchemaUtils.drop(DMLTestsData.Cities)
}

assertEquals(3, DMLTestsData.Cities.selectAll().count())
assertEqualLists(listOf("city1", "city4", "city5"), DMLTestsData.Cities.selectAll().map { it[DMLTestsData.Cities.name]})
SchemaUtils.drop(DMLTestsData.Cities)
}
}
}

0 comments on commit 12d970b

Please sign in to comment.