@TransactionalEventListener is a Spring annotation that makes an event listener run in relation to the outcome of the current transaction. By default the listener runs after the transaction commits, so side effects such as sending an email or publishing a message happen only when the data is really saved.
A plain @EventListener runs immediately when the event is published, even if the transaction later rolls back. That gap is the problem @TransactionalEventListener solves.
Imagine an order service that saves an order and publishes an OrderPlacedEvent. A normal @EventListener handles the event synchronously inside the same transaction. If it sends a confirmation email and the commit then fails, the customer receives an email for an order that does not exist.
@TransactionalEventListener binds the listener to a transaction phase through Spring's transaction synchronization. The event is held until the chosen phase is reached.
Publish the event from a transactional service and handle it in a separate bean.
Events can be plain records or classes; they do not need to extend ApplicationEvent.
The phase attribute selects when the listener runs.
| Phase | When it runs |
|---|---|
AFTER_COMMIT (default) | After a successful commit |
AFTER_ROLLBACK | After the transaction rolled back |
AFTER_COMPLETION | After commit or rollback |
BEFORE_COMMIT | Just before the commit, still able to affect the transaction |
If no transaction is active when the event is published, the listener is skipped and never called. Set fallbackExecution = true to run it immediately in that case.
This is the most common trap. In AFTER_COMMIT the original transaction is already committed but its resources may still be bound to the thread, so a repository call in the listener can join a transaction that will not commit again, and your write is silently lost. Start a new transaction explicitly.
Spring requires REQUIRES_NEW or NOT_SUPPORTED on a transactional listener that is annotated with @Transactional; other propagation settings fail at startup.
Add @Async to run the listener on another thread after the commit, which keeps slow work such as sending email out of the request. Enable it with @EnableAsync. Exceptions then no longer propagate to the publisher, so handle them inside the listener.
AFTER_COMMIT listener do not roll anything back, because the transaction is already finished.@Order.AFTER_COMMIT. The listener runs only after the surrounding transaction commits successfully.
No transaction was active when the event was published. Publish it from a @Transactional method, or set fallbackExecution = true.
Yes, but only in a new transaction, so annotate it with @Transactional(propagation = Propagation.REQUIRES_NEW).
No. It is in-process and not durable. Use an outbox table or a broker when events must survive a crash.
@TransactionalEventListener ties event handling to the transaction outcome. Use the default AFTER_COMMIT phase for notifications and integrations, use REQUIRES_NEW when the listener writes to the database, and add an outbox when delivery must be guaranteed.