Async processing issued in UOW

Hi all,

We have the following case:

  • Service which accepts the request and asynchronously process it (returns acknowledgment immediately, and after processing issues a command which contains the result: SupplyResultCommand)

  • Command for starting the business transaction: StartBusinessTransactionCommand

  • Command handler for StartBusinessTransactionCommand command which does the following:

  • Applies the event that business transaction is started: BusinessTransactionStartedEvent

  • Reaches out to multiple instances of Service and requests a processing

  • Event sourcing handler on BusinessTransactionStartedEvent which updates the aggregate (sets aggregate id among other updates)

Since command handler is in one UOW (Unit Of Work) meaning that transaction is not committed (not persisted to the database) until event sourcing handler finishes and services are reached out, there is a race condition that one of services issues a SupplyResultCommand before UOW is done (persistence to the database happened) causing exception that aggregate is not found. What is the best way (using Axon building blocks) to solve this issue?

Thank you!

Good thing is that UOW has a hook for the afterCommit. I moved Service calls to it and it solved the issue. On the positive side is that if something goes wrong in the afterCommit, command sender will be the one to handle the exception (the same situation as with service calls within the commit phase).

Is this THE way of solving this kind of problems?

The UnitOfWork.afterCommit() is indeed very suitable for triggering Asynchronous tasks that you don’t want executed if something is rolled back.
However, throwing exceptions for these handlers is something that may have unwanted side-effects. I am not sure if any remaining handlers are properly invoked.

I don’t have a full understanding of the case to provide any more details, at this point, but I hope this helps.