Quick revision
The short answer
Inside the transaction we publish an application event with ApplicationEventPublisher, and we handle it in a method annotated with @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT). Spring holds the event until the transaction commits and drops it if the transaction rolls back, so the email and the outgoing event are only sent for orders that really exist. Adding @Async keeps the caller fast, a listener that writes needs its own REQUIRES_NEW transaction, and for delivery that must survive a crash we use a transactional outbox instead.
What to remember
In this answer, we saw that side effects such as emails and Kafka messages cannot be rolled back, so they must wait for the commit. Publishing an application event inside the transaction and handling it with @TransactionalEventListener in the AFTER_COMMIT phase does that, @Async keeps the caller fast, a writing listener needs REQUIRES_NEW, and registerSynchronization gives the same hook in code. Because the event lives only in memory, guaranteed delivery needs a transactional outbox.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Inside the transaction we publish an application event with ApplicationEventPublisher, and we handle it in a method annotated with @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT). Spring holds the event until the transaction commits and drops it if the transaction rolls back, so the email and the outgoing event are only sent for orders that really exist. Adding @Async keeps the caller fast, a listener that writes needs its own REQUIRES_NEW transaction, and for delivery that must survive a crash we use a transactional outbox instead.
Why this question is asked
Sending an email from inside a transaction is a classic bug: the email goes out, then the transaction rolls back, and the customer gets a confirmation for an order that does not exist. Interviewers ask this question to see whether we know Spring's hook for running code after a commit and also its limits. In this answer, we will reproduce the problem, fix it with a transactional event listener, look at threads and transactions inside the listener, see the programmatic version, and discuss when we need an outbox.
1Reproducing the Problem
Here is the naive version. Everything runs in the order transaction, including the two side effects that cannot be undone.
A database rollback only undoes database changes. The email server and the Kafka broker are outside the transaction, so whatever we sent to them stays sent. We need those two calls to happen only once we know the commit succeeded.
2Publishing an Event Inside the Transaction
Spring's ApplicationEventPublisher lets a bean announce that something happened, as a plain Java object. Publishing is cheap and has no side effects of its own. The order service now says what happened and leaves the reaction to someone else.
Notice that the event carries the id and the email, not the Order entity. The listener may run on another thread or after the persistence context is closed, so plain values are safer than a managed entity.
3Handling It After Commit
A @TransactionalEventListener is an event listener that is tied to the publisher's transaction. With phase = AFTER_COMMIT, which is also the default phase, Spring does not call it when the event is published. It registers the event with the current transaction and calls the listener only after the commit succeeds. If the transaction rolls back, the event is discarded.
7 messages in order. Play them, or step through them one at a time.
On rollback the registered event is dropped and the listener is never called.
4Threads and Transactions in the Listener
By default the listener runs on the same thread as the caller, right after the commit, so a slow email server delays the HTTP response. Adding @Async, with @EnableAsync in the configuration, hands it to a Spring thread pool, so the caller returns as soon as the commit is done.
There is a catch if the listener writes to the database, for example to record that the email was sent. At the AFTER_COMMIT point the original transaction has committed, but its resources can still be bound to the thread, and data access code would join it without anything committing afterwards. So a listener that writes must declare @Transactional(propagation = Propagation.REQUIRES_NEW) to get a real transaction of its own. Spring Framework 6.1 and later checks this at startup and rejects a transactional event listener with a plain @Transactional, accepting REQUIRES_NEW or NOT_SUPPORTED (check your version).
If the event is sometimes published with no transaction at all, the listener is skipped by default. Setting fallbackExecution = true makes it run immediately in that case.
5The Programmatic Hook
Underneath, the listener uses a transaction synchronization, a callback object registered with the current transaction. We can register one ourselves when an event class feels too heavy.
The registration fails with an IllegalStateException if no transaction synchronization is active, which is a useful check that the method really runs in a transaction.
6What AFTER_COMMIT Does Not Guarantee
AFTER_COMMIT guarantees that we never send for a rolled-back order. It does not guarantee that we always send for a committed one. If the process crashes between the commit and the listener, or the Kafka send fails, the event is lost, because it only ever lived in memory.
When every committed order must produce exactly one published event, we use a transactional outbox: the event is inserted into an outbox table in the same transaction as the order, and a separate relay publishes it and retries until it succeeds. For a confirmation email, a rare lost message may be acceptable; for an event that drives billing, it usually is not.
7Follow-up questions
What other phases are there?
BEFORE_COMMIT runs just before the commit, still inside the transaction, so its writes are part of it and its exceptions roll it back. AFTER_ROLLBACK runs only after a rollback, which is useful for logging or alerts. AFTER_COMPLETION runs after either outcome.
Why not just send the email at the end of the @Transactional method?
Because the commit happens after the method returns, inside the proxy. Even the last line of the method runs before the commit, and the commit itself can still fail, for example on a constraint checked at commit time or an optimistic lock conflict.
Does an exception in an AFTER_COMMIT listener roll back the order?
No. The order is already committed. A synchronous listener's exception can still propagate to the caller, which may then report an error for an order that succeeded, so listeners should catch and log their own failures or run with @Async.
8Conclusion
In this answer, we saw that side effects such as emails and Kafka messages cannot be rolled back, so they must wait for the commit. Publishing an application event inside the transaction and handling it with @TransactionalEventListener in the AFTER_COMMIT phase does that, @Async keeps the caller fast, a writing listener needs REQUIRES_NEW, and registerSynchronization gives the same hook in code. Because the event lives only in memory, guaranteed delivery needs a transactional outbox.
Open with
Inside the @Transactional method I would publish an application event with ApplicationEventPublisher, carrying the order id and the email rather than the entity. Then a separate bean handles it with @TransactionalEventListener and phase AFTER_COMMIT.
Then, if they want more
Spring registers the event with the transaction and only calls the listener after a successful commit; on rollback the event is dropped, so we never email a customer about an order that does not exist. I would add @Async so the email does not slow down the response.
If the listener writes to the database, it needs @Transactional with REQUIRES_NEW, because at that point the old transaction's resources may still be bound and would not commit again; recent Spring versions even reject a plain @Transactional there. The same hook is available programmatically through TransactionSynchronizationManager.registerSynchronization with an afterCommit method.
The limit is that AFTER_COMMIT only protects against sending too much. If the process crashes right after the commit or the Kafka send fails, the event is lost.
So for the Kafka event, which other services rely on, I would use a transactional outbox: write the event into an outbox table in the same transaction and let a relay publish it with retries.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- 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.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "After an order is committed you need to send an email and publish an event, but never if...": https://scrivio-interview-library.netlify.app/databases/transactions/48e543cde9/