Hi Allard,
I am using the JPA repository as defined using the <axon:jpa-saga-repository /> element.
I do believe that I understand how the sagas are matched to handle events. Is it true that I can have any number of saga definitions (as classes not instances) that are started from the same type of event/association value, ie: ValidateClientSaga.java, ValidateWidgetSaga.java, etc fired with the same OrderCreatedEvent and associated with the same orderId?
That said, here is how my saga fires for validating the client:
1. @StartSaga assocated with the orderId from an ClientCodeEstablishedEvent
2. schedule a ValidationScheduleEvent to fire in 50ms (this event is a protected static class defined in the saga class)
3. @SagaEventHandler associated with the orderId from the ValidationScheduleEvent
4. try/catch block to validate client with external system; command fired for either found/not found; exception caught and event rescheduled in 60 seconds
5. @EndSaga associated with orderId from the ClientFoundEvent
6. @EndSaga associated with orderId from the ClientNotFoundEvent
Let's assume that the external server is in good working order so that #4 never has to reschedule. And let's just pick the happy path that client was found.
That means that #5 will execute once the ClientFoundEvent has been fired from the aggregate. Saga should be done. And a query of the SAGA_ENTRY table does prove that the saga has been removed.
The above will also show in the logs that only one Unit of Work was created.
Now let's change the client code which will create a new saga starting with #1. However now there will be two threads which have a Unit of Work created. There will be two processes which fire up and try to validate the client. And then the original one will throw and exception that it the saga changes cannot be committed as it has already been committed.
And this will continue on and create additional Unit of Work each time I change my client ID.
But as I said this error and additional Unit of Work can be eliminated if, in #5 and #6, I unassociate the saga with all of its associated values. I will do another run through but it's like once a thread has been associated with a saga it remains associated even after the saga was to have ended.
Also, I am defining my executor as:
<task:executor id="clientSagaManagerExecutor" pool-size="30" />
And the saga manager:
<axon:saga-manager id="clientSagaManager" event-bus="eventBus" saga-repository="sagaRepository">
<axon:async processor-count="30" executor="clientSagaManagerExecutor" transaction-manager="transactionManager" />
<axon:types>
org.example.processing.ValidateClientSaga
</axon:types>
</axon:saga-manager>
Thanks,
Randy.