spring-projects / spring-projects/spring-framework

What is the design reason behind Spring not executing nested @TransactionalEventListener listeners?

Open
#35,395 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: data status: waiting-for-triage
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

Hello, Spring Team.

I initially posted this question on Stack Overflow, but a reviewer determined that it's a question only the Spring maintainers could answer, which has prompted me to open this issue.


I'm working with Spring's transactional events and have come across a situation where chaining BEFORE_COMMIT event listeners does not work as one might initially expect.

Here is an example scenario:

// (1)
@Transactional
public void saveFoo(Foo foo) {
    applicationEventPublisher.publishEvent(new FooEvent(foo));
}

// (2)
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
public void handle(FooEvent event) {
    barService.saveBar(new Bar(event));
}

// (3)
@Transactional
public void saveBar(Bar bar) {
    applicationEventPublisher.publishEvent(new BarEvent(bar));
}

// (4) but not working (not listening)
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
public void handle(BarEvent event) {
    quxService.saveQux(new Qux(event));
}

My Investigation

After debugging, I believe I've found the technical reason for this in TransactionSynchronizationUtils.triggerBeforeCommit.

public static void triggerBeforeCommit(boolean readOnly) {
    for (TransactionSynchronization synchronization : TransactionSynchronizationManager.getSynchronizations()) {
        synchronization.beforeCommit(readOnly);
    }
}

It appears that when the commit process begins, the list of currently registered TransactionSynchronization objects is captured. The triggerBeforeCommit method then iterates over this captured list.

Even if a new TransactionSynchronization is registered from within this loop, the list being iterated over has already been fixed. As a result, the newly registered TransactionSynchronization is not called during the current phase.

My Question

I understand what is happening, but I would like to understand the design rationale behind this behavior.

Why was this intentionally designed this way?

Is it to prevent potential infinite loops of events? Or is it to ensure that the set of operations within the commit phase remains deterministic and cannot be modified during its own execution?

I would appreciate it if anyone could share insights into the intended design or point to any relevant documentation or discussions on this topic.

Thank you!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with TransactionSynchronizationUtils.triggerBeforeCommit and the nested @TransactionalEventListener example described in the issue. Review how transaction synchronizations are captured and invoked, then check the surrounding transaction-event documentation for relevant guarantees. Done means providing a documented design rationale or maintainer explanation for the observed behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.