Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What does @TransactionalEventListener do, what are its phases, and what happens if the event is published outside a transaction?

@TransactionalEventListener holds an event back until a chosen phase of the publisher's transaction: AFTER_COMMIT by default, or AFTER_ROLLBACK, AFTER_COMPLETION or BEFORE_COMMIT. It works by registering a TransactionSynchronization on the current transaction when the event is published. If no transaction is active, the listener is not called at all unless we set fallbackExecution = true.

Why this question is asked

A plain @EventListener runs immediately, inside the publisher's transaction. That is wrong for side effects such as sending an email: if the transaction later rolls back, the customer has an email about an order that does not exist. @TransactionalEventListener solves this by tying the listener to the transaction's outcome. Interviewers ask about it because the phases and the no-transaction behaviour are easy to get wrong, and the mistakes are silent. In this answer, we will recap why the timing matters, look at each phase, see how Spring implements the delay, and then cover what happens without a transaction.

1Why the Timing of a Listener Matters

Suppose OrderService.place() saves an order and publishes OrderPlaced, and a listener sends a confirmation email. With a plain @EventListener, the email is sent while the transaction is still open. If anything later in place() throws, or the commit itself fails because of a constraint, the order is rolled back, but the email has already gone.

What we want is to send the email only once we know the order is in the database. @TransactionalEventListener lets us say exactly that.

Java
@Component
class ConfirmationEmail {
@TransactionalEventListener // phase = AFTER_COMMIT by default
void on(OrderPlaced e) {
mail.sendConfirmation(e.orderId());
}
}
// place() commits -> email sent after the commit
// place() rolls back -> no email

2The Four Phases

The phase attribute chooses when the listener runs relative to the transaction. AFTER_COMMIT, the default, runs only if the transaction committed successfully, which makes it the usual choice for side effects that must not happen on rollback, such as emails, cache evictions or notifications. AFTER_ROLLBACK runs only if the transaction rolled back, which suits compensating actions or alerting. AFTER_COMPLETION runs after either outcome, which suits cleanup such as releasing a resource. BEFORE_COMMIT runs just before the commit, while the transaction is still open.

BEFORE_COMMIT is different from the other three in an important way. It runs inside the transaction, so its database work is part of the same commit, and if it throws, the transaction rolls back. The three after-phases run once the outcome is decided, so nothing they do can change it.

TransactionPhase values
PhaseRuns whenCan it affect the commit?Typical use
BEFORE_COMMITjust before commit, inside the transactionyes: throwing rolls backfinal validation, flush
AFTER_COMMIT (default)after a successful commitnoemails, notifications
AFTER_ROLLBACKafter a rollbacknocompensation, alerts
AFTER_COMPLETIONafter commit or rollbacknocleanup

3How Spring Delays the Listener

Spring tracks the current transaction in thread-bound state managed by TransactionSynchronizationManager. Part of that state is a list of TransactionSynchronization callbacks, objects with methods such as beforeCommit() and afterCompletion(int status), which the transaction manager calls at those moments.

When we publish OrderPlaced, the multicaster hands it to the transactional listener's adapter. Instead of calling our method, the adapter registers a synchronization for this event and returns. Later, when the transaction manager commits, it calls the synchronization callbacks, and the one for our listener calls our method in the right phase.

Step throughAn AFTER_COMMIT listener
OrderServiceAdapterTransaction managerConfirmationEmail
publishEvent(OrderPlaced)
register a TransactionSynchronization
returns at once, listener not called
place() returns: commit
commit succeeds
afterCompletion(COMMITTED)
on(OrderPlaced)

7 messages in order. Play them, or step through them one at a time.

The listener runs during the commit processing, after the data is safely in the database.

4What Happens Without a Transaction

If the event is published when no transaction is active, there is nothing to register a synchronization with. The default behaviour is to drop the event for this listener: our method is never called, and nothing is logged at the default level.

Java
public void placeWithoutTx(long id) { // no @Transactional
orders.save(new Order(id));
publisher.publishEvent(new OrderPlaced(id));
}
// ConfirmationEmail.on(...) is never called
@TransactionalEventListener(fallbackExecution = true)
void on(OrderPlaced e) { ... }
// now it runs immediately when no transaction is active

This is a common source of missing emails after a refactoring that moved @Transactional to a different method. With fallbackExecution = true, the listener runs immediately when there is no transaction, which is reasonable when the publisher sometimes runs without one and the side effect is still wanted. We should choose it on purpose, though, because without a transaction the email may be sent for data that another failure will never save.

5One Thing to Remember About AFTER_COMMIT

An AFTER_COMMIT listener runs after the commit but still on the same thread, while the transaction's resources are being cleaned up. If the listener itself writes to the database through a repository with the default REQUIRED propagation, that write can join the finished transaction and never be committed. The fix is to run such work with @Transactional(propagation = REQUIRES_NEW). Also remember that the side effect is not atomic with the commit: if the JVM dies between the commit and the listener, the event is lost, so anything that must happen needs an outbox.

6Follow-up questions

Which phase would you use to validate state that must abort the transaction?

BEFORE_COMMIT. It runs inside the transaction just before the commit, so a listener can check the final state, for example that an order's lines add up, and throw an exception, which rolls the transaction back. The after-phases are too late, because the outcome is already decided.

Is an exception in an AFTER_COMMIT listener reported to the caller?

No, not as a failure of the transaction, which has already committed. Spring logs the exception from the synchronization callback, and the caller's method completes normally. So after-commit listeners need their own error handling, retry or outbox.

Can a @TransactionalEventListener be asynchronous?

Yes, by adding @Async: the listener is still triggered at the chosen phase, but its work runs on an executor thread. That frees the request thread and also means the listener is outside the original transaction, which is often what we want for after-commit side effects.

7Conclusion

In this answer, we saw that @TransactionalEventListener defers a listener to a phase of the publisher's transaction: AFTER_COMMIT by default, AFTER_ROLLBACK, AFTER_COMPLETION, or BEFORE_COMMIT, which still runs inside the transaction and can abort it. Spring implements it by registering a TransactionSynchronization when the event is published. Without an active transaction the listener is skipped unless fallbackExecution is true, and AFTER_COMMIT is the usual choice for side effects that must not happen on rollback.

What to say in the interviewAbout 80 seconds aloud

Open with

@TransactionalEventListener ties a listener to the outcome of the publisher's transaction. When the event is published, the listener is not called immediately. Instead, Spring registers a TransactionSynchronization on the current transaction, and the transaction manager calls it at the chosen phase.

Then, if they want more

The default phase is AFTER_COMMIT, which runs only after a successful commit, so it is the right choice for side effects like emails or notifications that must not happen if the order rolls back. AFTER_ROLLBACK runs only on rollback, for compensation or alerts. AFTER_COMPLETION runs after either outcome, for cleanup.

BEFORE_COMMIT runs just before the commit, still inside the transaction, so its writes are part of the commit and if it throws, the transaction rolls back; I would use that one for validation that must abort the transaction.

If the event is published with no active transaction, the listener is not called at all and nothing obvious is logged, which is a common cause of missing emails. Setting fallbackExecution to true makes it run immediately in that case.

Two caveats: an after-commit listener that writes to the database needs REQUIRES_NEW, because it would otherwise join the finished transaction, and the side effect is not atomic with the commit, so a crash in between loses it.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does @TransactionalEventListener do, what are its phases, and what happens if the...": https://scrivio-interview-library.netlify.app/spring-boot/spring-events/6a2834a364/