Currently spring transactions bind to ThreadLocal.
Are there any plans to support binding to Kotlin coroutines?
For example @Transactional over a suspend function maybe should
bind to the coroutine context regardless of the executing thread.
On a related note: Reactive Transaction Manager integrations for R2DBC and MongoDB are currently in progress. They bind their transactional state to Reactor's subscriber Context.
Depends on https://github.com/Kotlin/kotlinx.coroutines/issues/284 where a PR is WIP.
Moving this one to RC2 since Reactor - Coroutines context is not complete yet, see https://github.com/Kotlin/kotlinx.coroutines/issues/284#issuecomment-513540942 for more details.
@sdeleuze looks like the work in https://github.com/Kotlin/kotlinx.coroutines/issues/284 was done and released in kotlinx.coroutines 1.3.0-RC2
For @transactional to work with coroutines, it will require just updating to the new coroutines release or there is a work to be done from the spring framework side as well?
I think for annotations it should work with latest Spring Framework master and kotlinx.coroutines 1.3.0-RC2 (not tested yet, if you do please add a comment on this issue). For programmatic usage, I think a helper will be needed, maybe as TransactionalOperator / Flow extensions.
Looks like it is working only if a reactive(returning publisher) controller method is annotated with @transactional and the rest of the call chain is suspending functions.
Looks like the issue comes from https://github.com/spring-projects/spring-framework/blob/0d3fb0ee0f4c4ab9166b17168129f9769601d0bb/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java#L324-L329
when the return type of the suspending function is not Deferred or Flow(has no ReactiveAdapter), it gets executed as a regular transaction using ThreadLocal instead of Reactive transaction
Most helpful comment
Looks like the issue comes from https://github.com/spring-projects/spring-framework/blob/0d3fb0ee0f4c4ab9166b17168129f9769601d0bb/spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java#L324-L329
when the return type of the suspending function is not Deferred or Flow(has no ReactiveAdapter), it gets executed as a regular transaction using ThreadLocal instead of Reactive transaction