sqldelight icon indicating copy to clipboard operation
sqldelight copied to clipboard

Transaction: Support CoroutineContext implementation

Open hfhbd opened this issue 1 year ago • 3 comments

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

hfhbd avatar Nov 26 '23 14:11 hfhbd

Does ThreadLocal#asContextElement help here?

kevincianfarini avatar Dec 31 '23 16:12 kevincianfarini

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).

dellisd avatar Jan 03 '24 14:01 dellisd

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.

hfhbd avatar Jan 03 '24 14:01 hfhbd