Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Coroutines transactions #22915

Closed
gkdevone opened this issue May 7, 2019 · 11 comments
Closed

Add support for Coroutines transactions #22915

gkdevone opened this issue May 7, 2019 · 11 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Milestone

Comments

@gkdevone
Copy link

gkdevone commented May 7, 2019

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 7, 2019
@mp911de
Copy link
Member

mp911de commented May 7, 2019

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.

@sdeleuze sdeleuze added this to the 5.2 M3 milestone May 10, 2019
@sdeleuze sdeleuze added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 10, 2019
@sdeleuze
Copy link
Contributor

Depends on Kotlin/kotlinx.coroutines#284 where a PR is WIP.

@sdeleuze sdeleuze modified the milestones: 5.2 M3, 5.2 RC1 Jun 7, 2019
@sdeleuze
Copy link
Contributor

Moving this one to RC2 since Reactor - Coroutines context is not complete yet, see Kotlin/kotlinx.coroutines#284 (comment) for more details.

@sdeleuze sdeleuze modified the milestones: 5.2 RC1, 5.2 RC2 Jul 21, 2019
@the-fine
Copy link

@sdeleuze looks like the work in Kotlin/kotlinx.coroutines#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?

@sdeleuze
Copy link
Contributor

sdeleuze commented Aug 19, 2019

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.

@the-fine
Copy link

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.

@the-fine
Copy link

the-fine commented Aug 22, 2019

Looks like the issue comes from

if (this.reactiveAdapterRegistry != null) {
ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(method.getReturnType());
if (adapter != null) {
return new ReactiveTransactionSupport(adapter).invokeWithinTransaction(method, targetClass, invocation);
}
}

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

@jhoeller jhoeller added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Sep 2, 2019
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Sep 3, 2019
This commits adds Coroutines extensions for
TransactionalOperator.transactional that accept suspending lambda or
Kotlin Flow parameters.

@transactional on suspending functions is not supported yet, spring-projectsgh-23575
has been created for that purpose.

This commit also renames invokeHandlerMethod to invokeSuspendingFunction
in CoroutinesUtils.

Closes spring-projectsgh-22915
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Sep 3, 2019
This commits adds Coroutines extensions for
TransactionalOperator.transactional that accept suspending lambda or
Kotlin Flow parameters.

@transactional on suspending functions is not supported yet, spring-projectsgh-23575
has been created for that purpose.

This commit also renames invokeHandlerMethod to invokeSuspendingFunction
in CoroutinesUtils.

Closes spring-projectsgh-22915
@sdeleuze sdeleuze changed the title Spring transactions and Kotlin coroutines Add support for Coroutines transactions Sep 3, 2019
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Sep 3, 2019
This commits adds Coroutines extensions for
TransactionalOperator.transactional that accept suspending lambda or
Kotlin Flow parameters.

@transactional on suspending functions is not supported yet, spring-projectsgh-23575
has been created for that purpose.

Closes spring-projectsgh-22915
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Sep 4, 2019
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Sep 12, 2019
As a follow-up of spring-projectsgh-22915, the purpose of this commit is to improve
Coroutines programmatic transaction API to make it more consistent with
the Java one and more idiomatic.

For suspending functions, this commit changes the
TransactionalOperator.transactional extension with a suspending lambda
parameter to a TransactionalOperator.executeAndAwait one which is
conceptually closer to TransactionalOperator.execute Java API so more
consistent.

For Flow, the TransactionalOperator.transactional extension is correct
but would be more idiomatic as a Flow extension.

This commit also adds code samples to the reference documentation.

Closes spring-projectsgh-23627
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Sep 12, 2019
As a follow-up of spring-projectsgh-22915, the purpose of this commit is to improve
Coroutines programmatic transaction API to make it more consistent with
the Java one and more idiomatic.

For suspending functions, this commit changes the
TransactionalOperator.transactional extension with a suspending lambda
parameter to a TransactionalOperator.executeAndAwait one which is
conceptually closer to TransactionalOperator.execute Java API so more
consistent.

For Flow, the TransactionalOperator.transactional extension is correct
but would be more idiomatic as a Flow extension.

This commit also adds code samples to the reference documentation.

Closes spring-projectsgh-23627
sdeleuze added a commit that referenced this issue Sep 12, 2019
As a follow-up of gh-22915, the purpose of this commit is to improve
Coroutines programmatic transaction API to make it more consistent with
the Java one and more idiomatic.

For suspending functions, this commit changes the
TransactionalOperator.transactional extension with a suspending lambda
parameter to a TransactionalOperator.executeAndAwait one which is
conceptually closer to TransactionalOperator.execute Java API so more
consistent.

For Flow, the TransactionalOperator.transactional extension is correct
but would be more idiomatic as a Flow extension.

This commit also adds code samples to the reference documentation.

Closes gh-23627
@paulschuetz
Copy link

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

@sdeleuze
Copy link
Contributor

It is implemented.

@paulschuetz
Copy link

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

@lenguyenthanh
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

8 participants