Description
Currently, Spring invokes @EventListener and @TransactionalEventListener methods once per published event. When multiple events of the same type are published within a single transaction, each results in a separate listener invocation.
It would be useful to support batched event delivery, allowing listeners to receive all events of the same type published during the transaction as a single List
Use Case
Within a transactional service, multiple updates are performed, and corresponding domain events are published. Each event triggers a separate listener call, resulting in multiple update statements and row locks.
Batch delivery would allow a single @TransactionalEventListener(phase = BEFORE_COMMIT) method to process all pending events together, enabling a single batched update.
Proposed Enhancement
Support for batched listener signatures such as:
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
public void handleEvents(List<AppEvent> events) {
batchUpdate(events);
}
The framework could accumulate events per transaction (e.g., using TransactionSynchronizationManager) and dispatch them together before commit.
Benefits
- Reduces redundant database updates
- Minimizes row locking and improves transaction efficiency
- Simplifies batching logic for domain event processing