https://kotlinlang.org logo
#exposed
Title
# exposed
a

Alexander Weickmann

09/23/2019, 1:26 PM
Hi all, it should be possible to write data and then immediately read that data within the same transaction right? In our code, this works except for one special case, in which the data only shows after the transaction block completes. I don't fully understand the "Working with Coroutines" section in the wiki where it says: " If you want to use the result and execute another queries within the same transaction you can use andThen function."
Copy code
val launchResult = suspendedTransactionAsync(<http://Dispatchers.IO|Dispatchers.IO>, db = db) {
    FooTable.selectAll().count()
}.andThen { count ->
    BarTable.select { BarTable.value eq count }.map { it[BarTable.name] }
}
here it seems that the result of the count is only available to the remaining code when passed via andThen function. when exactly is it necessary to use the andThen function? would this not work?
Copy code
val launchResult = suspendedTransactionAsync(<http://Dispatchers.IO|Dispatchers.IO>, db = db) {
    val count = FooTable.selectAll().count()
   BarTable.select { BarTable.value eq count }.map { it[BarTable.name] }
}
t

tapac

09/23/2019, 4:57 PM
As I can understand from the way Coroutines works the second example will execute the whole block in one pass and will block the thread for all that time while in the first example the thread will be returned into pool before making second request and might help to dispatch some other coroutines which executing on the same dispatcher.
a

Alexander Weickmann

09/25/2019, 4:15 PM
Hm. What does that help? The work does not really go away and it's on the IO thread pool anyways ...
Anyways we found the source of our issue. It's that we used the DAO api to read data, and in another place we insert the data directly via the table
2 Views