Saga/Aggregate: multiple threads and locking

Thanks very much Steven.

For my own future reference (I know I will end up asking myself this question again in the future), the classes you referred to are:

  • Aggregate:
    • modelling:org.axonframework.modelling.command.LockingRepository (abstract class)
    • axon-eventsourcing:org.axonframework.eventsourcing.EventSourcingRepository (extends LockingRepository)
    • you can see where the lock is obtained by searching: lockFactory.obtainLock in LockingRepository.
  • Saga:
    • modelling:org.axonframework.modelling.saga.repository.LockingSagaRepository (abstract class)
    • modelling:org.axonframework.modelling.saga.repository.AnnotatedSagaRepository (extends LockingSagaRepository)
    • you can see where the lock is obtained by searching for invocations to the lockSagaAccess method in the LockingSagaRepository (whenever load or createInstance is called).

And so as I have understood it:

  • Concurrent access to state within an aggregate/saga will never occur within the same JVM.
  • This is because only one unit of work (within the same JVM process) can possess the lock for an aggregate/saga at a time.
  • Other unit of work instances in the JVM for the same aggregate/saga may exist but they will block until they are able to grab the lock.

Is this an accurate summary?

(I’m assuming Axon framework defaults here i.e. Pessimistic Locking)