Hello all,
I have a situation where my controller uses the gateway to send a CreateParentCommand. The ParentAggregate constructor handles the command and applies the ParentCreatedEvent.
Now I need to handle that event somewhere else and use the gateway to send a CreateChildCommand (the ChildAggregate already has a handler constructor where the ChildCreatedEvent is applied).
I’m not sure what the best approach is for that, but here’s how I tried to handle it:
I created a CreateParentSaga that handles the ParentCreatedEvent and sends the CreateChildCommand through the autowired command gateway. The associationProperty on the Saga is set to the guid property of ParentCreatedEvent. I annotated this saga event handler as @StartSaga and @EndSaga.
It all works and the events get published, but I see that a separate Transaction being created for each of the 2 commands I am sending through the gateway, when I hoped only one transaction would be used.
I tried this using JPA default configuration as the Event Sourcing storage and I see two transactions being started.
I also tried it with my own implementation of the EventStore interface. In this case, I have control of the commit method that I override on my implementation and I can see it being hit 3 times. The first time with a collection of the 2 events that were applied and then 1 more time for each of the individual events. My storage engine is Greg Young’s Event Store and it doesn’t duplicate the events, so all is fine in that aspect. The default JPA implementation also doesn’t duplicate them for that matter (I’m assuming that’s because the sequence number is the same…).
But I still would like it if that commit was only fired once, taking in a collection with 2 events…
Maybe if the command bus used the same transaction for each command or the same unit of work the commit would only be fired once.
I’m not entirely sure, but I have the impression that the commit is fired 3 times because 3 units of work have been registered, 1 for the saga, 1 for the ParentAggregate and 1 for the ChidAggregate.
Any help would be appreciated,
Bruno