Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marking event as completed may fail when used with ChainedTransactionManager #886

Open
maciejwalkowiak opened this issue Oct 16, 2024 · 1 comment
Assignees
Labels
in: event publication registry Event publication registry meta: waiting for feedback Waiting for feedback of the original reporter

Comments

@maciejwalkowiak
Copy link
Contributor

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.

@maciejwalkowiak maciejwalkowiak changed the title Marking event as completed mail fail when used with ChainedTransactionManager Marking event as completed may fail when used with ChainedTransactionManager Oct 17, 2024
@odrotbohm
Copy link
Member

I'll have to look into the example for specifics, but generally speaking, ChainedTransactionManager is not something we support explicitly. In fact, it's been previously used to achieve orchestration similar to what we now achieve with the event publication registry. I guess what I'd recommend in a scenario like this (two DataSources I assume) is to commit on the first and have an @ApplicationModuleListener overriding the @Transactional to the second transaction manager.

@odrotbohm odrotbohm self-assigned this Oct 18, 2024
@odrotbohm odrotbohm added in: event publication registry Event publication registry meta: waiting for feedback Waiting for feedback of the original reporter labels Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: event publication registry Event publication registry meta: waiting for feedback Waiting for feedback of the original reporter
Projects
None yet
Development

No branches or pull requests

2 participants