spring-projects / spring-projects/spring-modulith

Marking event as completed may fail when used with `ChainedTransactionManager`

Open
#886 1 comment 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Oct 18, 2024.

in: event publication registry meta: waiting for feedback
Dominant language
Java
Stars
1.2k
Forks
222
PR merge metrics
No merged PRs in 30d

Description

I run into a following issue:

  • event is published, then executed but not marked as completed
  • the scheduled job that resubmits incomplete publications run, executes same event again and successfully marks event as completed

Project has a little unusual setup:

  • there are two datasources, each has its own transaction manager, one transaction manager is the default one and marked as @Primary
  • there is one extra transaction manager org.springframework.data.transaction.ChainedTransactionManager configured for two transactions managers mentioned above

The code looks more or less like this:

@Service
public class BarService {
    private final FooService fooService;

    @Transactional(transactionManager = "chainedTransactionManager")
    void bar() {
        fooService.foo();
    }
}

@Service
public class FooService {
    private final ApplicationEventPublisher eventPublisher;

    @Transactional
    void foo() {
        SomethingHappened event = new SomethingHappened(UUID.randomUUID().toString());
        eventPublisher.publishEvent(event);
    }
}

@Component
public class FooListener {

    @ApplicationModuleListener
    void handle(SomethingHappened event) {
        LOGGER.info("Handling: {}", event);
    }
}

When barService.bar() is called, in logs I can see that handling of the event in the listener may happen after FooService#foo method finishes and before BarService#bar finishes, but the event publication is inserted after BarService#bar method finishes execution. So there is a chance, that marking event as completed happens before the event publication is inserted.

Sample that reproduces this issue: https://github.com/maciej-scratches/modulith-chainedtransaction-manager-issue/blob/main/src/test/java/org/example/FooServiceTest.java#L43

Due to asynchronous nature, it's not easy to reproduce. When test runs 100 times, on my machine usually fails once or twice.

Considering that ChainedTransactionManager is deprecated perhaps this issue is irrelevant, but I am raising it as it maybe affects also other arrangements I am not aware of - if this is not the case - feel free to close it.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.