When using @TransactionalEventListener with the default AFTER_COMMIT or AFTER_COMPLETION phase, any database writes in the listener method are being discard silently, e.g.:
@TransactionalEventListener
void onEvent(BookSavedEvent event) {
// This does get called, but the following write to the database will be discarded silently
var book = books.findById(event.bookId).orElseThrow();
book.title = "New Title";
books.save(book);
}
This problem doesn't happen if the phase is BEFORE_COMMIT or if just using the regular @EventListener instead.
I've made an example project using v2.5.0 demonstrating the problem: https://github.com/laech/spring-transactional-event-listener-disappearing-writes/blob/master/src/main/java/com/example/demo/DemoApplication.java
If you do ./gradlew bootRun on the above project you can see the mismatch in expectation.
When using
@TransactionalEventListenerwith the defaultAFTER_COMMITorAFTER_COMPLETIONphase, any database writes in the listener method are being discard silently, e.g.:This problem doesn't happen if the phase is
BEFORE_COMMITor if just using the regular@EventListenerinstead.I've made an example project using v2.5.0 demonstrating the problem: https://github.com/laech/spring-transactional-event-listener-disappearing-writes/blob/master/src/main/java/com/example/demo/DemoApplication.java
If you do
./gradlew bootRunon the above project you can see the mismatch in expectation.