Skip to content

Document transactional semantics for @TransactionalEventListener after completion methods #26974

@laech

Description

@laech

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.

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)status: backportedAn issue that has been backported to maintenance branchestype: documentationA documentation task

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions