Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

An AFTER_COMMIT @TransactionalEventListener saves an AuditRecord with a Spring Data repository. No error is logged, but the row never appears in the database. Explain the cause and the fix.

At AFTER_COMMIT the original transaction has committed, but its resources, such as the EntityManager and connection, are still bound to the thread. The repository's save() uses REQUIRED propagation, so it joins that finished transaction, and nothing ever commits the new write, which is why no error appears and no row is stored. The fix is to run the listener's work in a new transaction with @Transactional(propagation = REQUIRES_NEW), or hand it to an async executor, remembering that the audit write is still not atomic with the original commit.

Why this question is asked

This bug is especially unpleasant because nothing fails: the listener runs, save() returns the entity as if all went well, the logs are clean, and the table stays empty. Interviewers like it because the cause sits in a detail of how Spring finishes a transaction, and the explanation shows whether we really understand propagation. In this answer, we will recap what happens at the end of a transaction, reproduce the missing row, explain why the write is never committed, apply the fix, look at a newer Spring version check that catches part of the problem, and finally discuss the crash window that remains.

1A Quick Recap of the End of a Transaction

When a @Transactional method returns, the transaction manager commits. For JPA, that means flushing pending changes from the EntityManager, the persistence context that tracks our entities, and committing the database connection. After the commit, the manager runs the after-completion callbacks registered on the transaction, and only after those does it clean up: closing the EntityManager and unbinding the resources from the thread.

An AFTER_COMMIT @TransactionalEventListener is one of those callbacks. So when our listener runs, the commit has happened, but the transaction's resources are still bound to the current thread and Spring still considers itself inside that transaction's scope.

2Reproducing the Missing Row

Here is the code from the question in a minimal form.

Java
@Service
class OrderService {
@Transactional
public void place(long id) {
orders.save(new Order(id));
publisher.publishEvent(new OrderPlaced(id));
}
}
@Component
class AuditListener {
@TransactionalEventListener // AFTER_COMMIT
void on(OrderPlaced e) {
AuditRecord saved = audits.save(new AuditRecord(e.id())); // returns normally
log.info("saved {}", saved); // saved AuditRecord[...]
}
}
// SELECT count(*) FROM audit_record; -- 0

Notice that the order row is there, because its transaction committed before the listener ran. The audit row is not, and no exception was thrown anywhere.

3Why the Write Is Never Committed

Spring Data repository methods such as save() are themselves @Transactional with the default propagation, REQUIRED. REQUIRED means: if a transaction is already active on this thread, join it; otherwise start a new one. When save() is called inside our listener, the transaction manager looks at the thread and finds the original transaction's resources still bound. So save() joins that transaction instead of starting one.

But that transaction has already committed and will never commit again. With JPA, save() calls persist(), which puts the AuditRecord into the EntityManager, and the insert would only be sent at the next flush or commit. Neither comes: after the callbacks the manager just closes the EntityManager and the pending insert is thrown away. With an IDENTITY id, Hibernate sends the insert at once to get the id, but on a connection whose transaction is never committed again, and a pool such as HikariCP typically rolls back uncommitted work when the connection is returned, so the row is lost either way. Spring's own documentation for TransactionSynchronization warns about exactly this: at after-completion time, data access code still participates in the original transaction, but its changes will not be committed.

Step throughThe audit write joins a finished transaction
OrderServiceTx managerAuditListenerEntityManager
place() returns: commit
flush and commit order 42
afterCompletion: on(OrderPlaced)
save(AuditRecord): REQUIRED joins the old tx
persist queued, no flush
cleanup: close EntityManager
pending insert discarded

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

Nothing ever flushes or commits the audit insert, so it disappears without an error.

4The Fix: REQUIRES_NEW or an Executor

We need the audit write to have a transaction of its own. REQUIRES_NEW means: always start a new transaction, suspending any existing one. Putting it on the listener gives the listener a fresh transaction that commits when the listener returns.

Java
@Component
class AuditListener {
@TransactionalEventListener
@Transactional(propagation = Propagation.REQUIRES_NEW)
void on(OrderPlaced e) {
audits.save(new AuditRecord(e.id()));
}
}
// SELECT count(*) FROM audit_record; -- 1

Notice that the listener must go through Spring's proxy for @Transactional to apply, which it does, because the event adapter calls it on the bean from the context. An alternative is to add @Async, so the listener runs on an executor thread where no transaction is bound; then save() starts its own transaction normally. Both work. REQUIRES_NEW keeps the work on the request thread and makes the intent explicit; @Async also frees the request thread but moves the work out of the request's context.

Recent Spring versions help here. Since Spring Framework 6.1, a @TransactionalEventListener method that is also annotated with @Transactional must use REQUIRES_NEW or NOT_SUPPORTED, or the application fails at startup with an error saying so. That catches the case where someone adds plain @Transactional and expects it to work. It does not catch our original bug, where the listener has no @Transactional at all and only the repository does, so we still need to know the reason. Check your version for the exact behaviour.

5The Crash Window That Remains

Even with the fix, the audit write happens after the order's commit, in a separate transaction. If the JVM dies between the two, or the audit insert fails, the order exists without its audit record, and nothing retries it. For an audit trail, that is often unacceptable.

If the audit must be reliable, the simplest option is to write the audit row in the same transaction as the order, either directly in place() or with a BEFORE_COMMIT listener, so both commit or neither does. When the audit work must stay separate, for example because it calls another system, we use a transactional outbox: write an outbox row with the order, and let a relay process it later with retries.

6Follow-up questions

How would you make the audit write reliable across a crash?

Make it part of the original transaction. Either insert the audit row in place() or use a BEFORE_COMMIT listener, so the audit commits atomically with the order. If the audit involves another system, store an outbox row in the original transaction and process it asynchronously with retries; Spring Modulith's event publication registry provides this for in-process listeners.

Would JdbcTemplate instead of JPA behave differently?

The details differ, but the problem is the same. With JdbcTemplate, the insert is sent at once on the connection that belongs to the finished transaction, and whether it is then committed depends on what happens when Spring resets the connection and returns it to the pool: resetting auto-commit may commit it, a pool may roll it back. The outcome depends on the driver and pool, so we must not rely on either; the work should run in a transaction of its own.

Why does Spring not simply start a new transaction automatically in after-commit callbacks?

Because the callback API is general: Spring cannot know whether the listener's work should be transactional at all, or how. It documents the behaviour and leaves the propagation choice to us, and since 6.1 it rejects the combinations that cannot work.

7Conclusion

In this answer, we saw that an AFTER_COMMIT listener runs after the commit but while the transaction's resources are still bound to the thread. A repository call with REQUIRED propagation joins that finished transaction, its insert is never flushed or committed, and it disappears without an error. Running the listener with REQUIRES_NEW or on an async executor fixes it, Spring 6.1 and later reject a plain @Transactional on such listeners, and because the audit is still not atomic with the order, a crash between them needs a same-transaction write or an outbox to be safe.

What to say in the interviewAbout 90 seconds aloud

Open with

The listener runs after the commit, but still inside the transaction's cleanup phase. The commit has happened, but the EntityManager and connection are still bound to the thread until the after-completion callbacks finish.

Then, if they want more

The repository's save is transactional with REQUIRED propagation, so it looks at the thread, finds the existing transaction and joins it instead of starting a new one. But that transaction has already committed and will never commit again, so the persisted AuditRecord is never flushed, and when Spring closes the EntityManager the insert is discarded.

That is why there is no error and no row. Spring's documentation warns about exactly this for after-completion callbacks. The fix is to give the listener its own transaction: annotate it with @Transactional with propagation REQUIRES_NEW, or make it @Async so it runs on another thread where save starts a fresh transaction.

Since Spring 6.1, putting a plain @Transactional on a transactional event listener fails at startup unless it is REQUIRES_NEW or NOT_SUPPORTED, but that does not catch the case where only the repository is transactional. I would also point out that even with the fix the audit is not atomic with the order.

If the JVM dies between the commit and the listener, the audit is lost, so if it must be reliable I would write it in the same transaction, for example with a BEFORE_COMMIT listener, or use an outbox.

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.
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 "An AFTER_COMMIT @TransactionalEventListener saves an AuditRecord with a Spring Data...": https://scrivio-interview-library.netlify.app/spring-boot/spring-events/ced07e0bfa/