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.

Comment From: mp911de

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.

Comment From: sdeleuze

Depends on https://github.com/Kotlin/kotlinx.coroutines/issues/284 where a PR is WIP.

Comment From: sdeleuze

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.

Comment From: the-fine

@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?

Comment From: sdeleuze

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.

Comment From: the-fine

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.

Comment From: the-fine

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

Comment From: paulschuetz

Hi, what is the state of this feature? :slightly_smiling_face: Is it already implemented or do we still have to use TransactionalOperator for transactions?

Comment From: sdeleuze

It is implemented.

Comment From: paulschuetz

Well then I would be glad if you could help me with my issue here: https://stackoverflow.com/questions/68590209/spring-transactional-on-suspend-function

Comment From: lenguyenthanh

Hi @paulschuetz did you figure out how to solve your issue? I'm having a similar problem. Thanks!