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

@Transactional support for coroutines in WebMVC + JDBC projects #26705

Closed
mistahenry opened this issue Mar 19, 2021 · 2 comments
Closed

@Transactional support for coroutines in WebMVC + JDBC projects #26705

mistahenry opened this issue Mar 19, 2021 · 2 comments
Assignees
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@mistahenry
Copy link

I upgraded to Spring Boot 2.4 and Spring 5.3 in my Kotlin Web MVC project after seeing:

Support for @transactional suspending functions (Kotlin Coroutines)

This transactional support, though, is not working for me in my project where I'm using jdbcTemplate everywhere. Here's an example

An @Controller suspending endpoint:

@PostMapping("/orders")
suspend fun saveOrder(@RequestBody orderRequest: OrderRequest): ResponseEntity<Order>{
   val order = orderService.saveOrder(orderRequest)
   // ...
}

Service:

@Transactional 
@Service
OrderServiceImpl(): OrderService{
   override suspend fun orderService(request: OrderRequest): Order{
       // a mix of WebClient external service calls and calls to dao's that do coroutine jdbc in the `withContext(Dispatchers.IO)`
   }
}

In a simple test, my DB calls are not in the same physical transaction. After looking more closely at at this issue, I will only get the @Transactional support if the determined TransactionManager is an instance of ReactiveTransactionManager

When debugging TransactionAspectSupport.invokeWithinTransaction, I always get back the JdbcTransactionManager. Is it not possible to use declarative transactions in Web MVC with coroutines as I've described? I had assumed this enhancement took to managing the ThreadLocal based reference to the open transaction and added it to the current CoroutineContext but that isn't the case for me.

If it's not possible, then what are the caveats around this @Transactional coroutine support? Must I be using Spring WebFlux? Is there a requirement around the return type?

If there's an enhancement to be made here, I'd be happy to take a stab at it once I better understand what the desired state of this is.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 19, 2021
@mistahenry
Copy link
Author

@sdeleuze whenever you have a chance, could you please provide some clarity here as to what's expected/possible and if there's an enhancement to be made here to allow coroutines to work with @Transactional in the way I've described?

It's hard for me to tell whether I'm encountering a bug, something unsupported, or something not yet supported.

@sdeleuze sdeleuze self-assigned this Apr 1, 2021
@sdeleuze
Copy link
Contributor

sdeleuze commented Apr 9, 2021

Coroutines are leveraging the Reactive transaction support, as a consequence they are not designed to work with thread-bound transactions. So Coroutines transactions should be used with WebFlux and R2DBC, not WebMVC and JDBC.

@sdeleuze sdeleuze closed this as completed Apr 9, 2021
@sdeleuze sdeleuze added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

3 participants