Course outline

Spring Boot / Advanced: observability and resilient releases / Lesson 21

Transaction-bound events and durable delivery

Run an event listener at a transaction phase and account for failures that remain after commit.

About 4 min readContent reviewed
On this page

Lesson goals

  • Explain transaction-bound listener phases and fallback behavior.
  • Publish an event and handle it after a successful commit.
  • Design durable recovery and duplicate handling when an event cannot be lost.

Choose the transaction phase

A listener that reads a newly written record can observe incomplete state if it runs before the transaction commits. Spring’s TransactionalEventListener can bind a listener to a transaction phase, with AFTER_COMMIT as the default. If no transaction is active, the listener is not called by default unless fallback execution is explicitly enabled. Establish which transaction manager and execution model the application uses before relying on this ordering.

Publish within the intended transaction

The service fragment assumes an existing repository, event type and application event publisher. It saves an order and publishes an event carrying a stable identifier within the transactional method. The annotation must be invoked through the configured transactional boundary; an internal self-call may not cross a proxy. Test rollback and commit through the real Spring bean rather than constructing the service directly and assuming transaction interception.

java Publish an event in a transactional service
@Transactional
public void placeOrder(Order order) {
    orders.save(order);
    events.publishEvent(new OrderCreated(order.getId()));
}

Order, OrderCreated and repository methods are application-specific. The transaction requires the configured Spring boundary.

Handle the committed event

The listener fragment registers an after-commit handler for the application-defined event. Its service dependency and failure handling are deliberately omitted. The callback is tied to the transaction phase, not a durable message broker. A failure after commit does not undo the already committed order. If the handler needs a separate database transaction, define and test that boundary explicitly instead of assuming the original commit can include later writes.

java Handle the event after commit
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void afterOrder(OrderCreated event) {
    notificationService.handle(event.orderId());
}

The listener illustrates phase selection. It does not implement durable messaging or an independent database transaction.

Close the delivery gap

A process may stop after the database commits but before an in-memory listener completes its external effect. An after-commit listener solves an ordering problem, not guaranteed delivery. When the event must survive process failure, persist an outbox intent in the same database transaction as the business change and use a recoverable publisher. Consumers should deduplicate stable event IDs because a publisher can send successfully and crash before recording completion.

Prove the intended boundaries

In a disposable integration environment, verify the listener’s behavior after commit, after rollback and when no transaction is active under the selected fallback configuration. Simulate callback failure and inspect the committed database state. If using an outbox, test recovery after commit and duplicate delivery after an uncertain publish. A mocked publisher or direct method call cannot establish real transaction interception, broker delivery or process-crash recovery.

Common errors and fixes

The listener runs without the intended transaction

Why it happens
The service was invoked outside the proxy boundary or fallback was misunderstood.
How to fix it
Invoke through the configured transactional bean and define fallback deliberately.
Prevent it next time
Test commit, rollback and no-transaction cases.

A callback failure is expected to undo a committed order

Why it happens
After-commit work is outside the completed commit.
How to fix it
Handle failure and recovery as a separate boundary.
Prevent it next time
Force listener failure and inspect committed state.

A critical event disappears on process crash

Why it happens
An in-memory listener was treated as a durable queue.
How to fix it
Persist an outbox intent with the business write.
Prevent it next time
Test publisher recovery and duplicate delivery.

2 practice exercises

1. What happens by default when a transaction-bound listener receives an event with no active transaction?

Show worked solution

It is not invoked unless fallback execution is enabled. Verify this against the configured transaction manager and test the no-transaction path rather than assuming an ordinary event listener’s behavior.

2. A process crashes after commit and before a listener sends a notification. Is after-commit ordering sufficient?

Show worked solution

No. The callback is not durable. Store an outbox intent in the business transaction, run a recoverable publisher and make the notification effect idempotent or reconcilable.

Key takeaways

  • Transaction-bound listeners provide ordering, not durable delivery.
  • Test proxy invocation, transaction outcome and recovery independently.

Official references