-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Document that @TransactionalEventListener
only works with non-reactive transactions
#25805
Comments
Duplicate of https://jira.spring.io/browse/DATAMONGO-2632. Reactive event listeners do not participate in the transaction that published the event because there's no mechanism to propagate the transaction context to |
Thanks for letting us know about the duplicate @mp911de. We'll use this issue to document the support a bit more explicitly. |
@snicoll @mp911de @jhoeller |
As of 6.1, |
@TransactionalEventListener
only works with non-reactive transactions
Affects: 5.2.9
The problem described here can be reproduced using the following sample project: https://github.com/codependent/transactional-event-sample
In a Spring Boot Webflux application with Reactive Mongodb as repository, I would like to take advantage of Spring's event publishing in a transactional way, thus I tried using
TransactionalEventListener
. The problem is even though theReactiveTransactionManager
has started an actual transaction, when dealing with the event,ApplicationListenerMethodTransactionalAdapter.onApplicationEvent()
considers bothTransactionSynchronizationManager.isSynchronizationActive()
andTransactionSynchronizationManager.isActualTransactionActive()
as false.Below you can see the logs of this process:
The first four lines show that there is an actual transaction, however, inside my transactional method I print the following (5th log line):
which shows
create() isSyncActive false - isTxActive false
. I seems thatTransactionSynchronizationManager
isn't considering the ongoing Reactive Mongo transaction and in the 6th log line, whenApplicationListenerMethodTransactionalAdapter.onApplicationEvent()
kicks in, it doesn't synchronize the transaction, and just skips it.MongoDb Transaction config:
Service and event listener:
In order to use the sample you need a Mongodb 4.x instance with replication enabled (to support transactions), e.g. Mongo Atlas, configuring the appropriate value in application.yml:
After starting the application just call:
curl -X POST localhost:8080/users -d '{"name": "John Doe"}' -H "content-type: application/json"
The text was updated successfully, but these errors were encountered: