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

Filip Lastic

08/08/2022, 1:31 PM
[Sharing transactions between threads] Hello. I know that the documentation contains this message:
you should not try to share a transaction between multiple threads as it will lead to undefined behaviour.
, but we have one usecase where we need to share transactions between threads. We have GraphQL DataLoaders https://opensource.expediagroup.com/graphql-kotlin/docs/server/data-loader/. DataLoader can be executed inside another thread and we have to call database query inside it. We solved the issue by using nested transactions and using wrapper on our code:
Copy code
TransactionManager.manager.bindTransactionToThread(outerTx)
return synchronized(outerTx) {
    transaction {
        TransactionManager.manager.bindTransactionToThread(this)
        statement()
    }
}
Do you think this this is a totally anti-pattern and we should do this in some other way or it is an acceptable solution?
5 Views