sqldelight
sqldelight copied to clipboard
Transaction: Support CoroutineContext implementation
SQLDelight Version
2.0.0
Application Operating System
JVM - R2DBC
Describe the Bug
I want to improve the current R2DBC driver by using coroutines instead of a ThreadLocals.
Unfortunately, our current runtime api is not compatible with composing CoroutineScope, the api requires returning a Transacter.Transaction
, which breaks withContext(CoroutineTransaction()) { }
, because the new transaction is only valid in the witContext
scope but it is not possible to fetch this api in currentTransaction
.
private class CoroutineTransaction: Transacter.Transaction(), CoroutineContext.Element { }`
Ideally, the driver should just support fun transaction(block: Transacter.Transaction.() -> R): R
directly and the transaction needs to notify the changed queries.
Unfortuantely, this will break the api/abi of our runtime. But the current design already has some problems regarding the async runtime.
I will create a prototyp with this change.
cc @dellisd
Stacktrace
No response
Does ThreadLocal#asContextElement
help here?
Unfortunately it does not, both because ThreadLocal
is JVM-only, and you can still have two transactions begin concurrently on a single thread (e.g. in JS where there is only ever one thread).
Yes, this one of the problems, another one is R2DBC does not link a transaction to a single thread. I will create a draft PR next days.