java.lang.Object
org.axonframework.modelling.saga.repository.jpa.JpaSagaStore
All Implemented Interfaces:
SagaStore<Object>

public class JpaSagaStore extends Object implements SagaStore<Object>
JPA implementation of the SagaStore. It uses an EntityManager to persist the actual saga in a backing store in serialized form.

After each operation that modified the backing store, EntityManager.flush() is invoked to ensure the store contains the last modifications. To override this behavior, see setUseExplicitFlush(boolean)

Since:
3.0
Author:
Allard Buijze
  • Constructor Details

  • Method Details

    • builder

      public static JpaSagaStore.Builder builder()
      Instantiate a Builder to be able to create a JpaSagaStore.

      The EntityManagerProvider and Serializer are hard requirements and as such should be provided.

      Returns:
      a Builder to be able to create a JpaSagaStore
    • loadSaga

      public <S> SagaStore.Entry<S> loadSaga(Class<S> sagaType, String sagaIdentifier)
      Description copied from interface: SagaStore
      Loads a known saga SagaStore.Entry instance with given sagaType and unique sagaIdentifier.

      Due to the concurrent nature of Sagas, it is not unlikely for a Saga to have ceased to exist after it has been found based on associations. Therefore, a repository should return null in case a Saga doesn't exists, as opposed to throwing an exception.

      Specified by:
      loadSaga in interface SagaStore<Object>
      Parameters:
      sagaType - The type of the returned saga entry
      sagaIdentifier - The unique identifier of the returned saga entry
      Returns:
      The saga entry, or null if no such saga exists
    • loadAssociationValues

      protected Set<AssociationValue> loadAssociationValues(jakarta.persistence.EntityManager entityManager, Class<?> sagaType, String sagaIdentifier)
      Loads the association values of the saga with given sagaIdentifier and sagaType.
      Parameters:
      entityManager - the entity manager instance to use for the query
      sagaType - the saga instance class
      sagaIdentifier - the saga identifier
      Returns:
      the associations of the given saga
    • removeAssociationValue

      protected void removeAssociationValue(jakarta.persistence.EntityManager entityManager, Class<?> sagaType, String sagaIdentifier, AssociationValue associationValue)
      Removes the given associationValue of the saga with given sagaIdentifier and sagaType.
      Parameters:
      entityManager - the entity manager instance to use for the query
      sagaType - the saga instance class
      sagaIdentifier - the saga identifier
      associationValue - the association value to remove
    • storeAssociationValue

      protected void storeAssociationValue(jakarta.persistence.EntityManager entityManager, Class<?> sagaType, String sagaIdentifier, AssociationValue associationValue)
      Stores the given associationValue of the saga with given sagaIdentifier and sagaType.
      Parameters:
      entityManager - the entity manager instance to use for the query
      sagaType - the saga instance class
      sagaIdentifier - the saga identifier
      associationValue - the association value to add
    • findSagas

      public Set<String> findSagas(Class<?> sagaType, AssociationValue associationValue)
      Description copied from interface: SagaStore
      Returns identifiers of saga instances of the given sagaType that have been associated with the given associationValue.
      Specified by:
      findSagas in interface SagaStore<Object>
      Parameters:
      sagaType - The type of the returned sagas
      associationValue - The value that the returned sagas must be associated with
      Returns:
      A set of identifiers of sagas having the correct type and association value
    • deleteSaga

      public void deleteSaga(Class<?> sagaType, String sagaIdentifier, Set<AssociationValue> associationValues)
      Description copied from interface: SagaStore
      Deletes a Saga with given sagaType and sagaIdentifier and all its associations. For convenience all known association values are passed along as well, which has the advantage that the saga store is not required to keep an index of association value to saga identifier.
      Specified by:
      deleteSaga in interface SagaStore<Object>
      Parameters:
      sagaType - The type of saga to delete
      sagaIdentifier - The identifier of the saga to delete
      associationValues - The known associations of the saga
    • updateSaga

      public void updateSaga(Class<?> sagaType, String sagaIdentifier, Object saga, AssociationValues associationValues)
      Description copied from interface: SagaStore
      Updates a given Saga after its state was modified. The tracking token of the event last handled by the Saga is also passed as a parameter. Note that the given token may be null if the Saga is not tracking the event store.
      Specified by:
      updateSaga in interface SagaStore<Object>
      Parameters:
      sagaType - The type of the Saga
      sagaIdentifier - The identifier of the Saga
      saga - The Saga instance
      associationValues - The initial association values of the Saga
    • insertSaga

      public void insertSaga(Class<?> sagaType, String sagaIdentifier, Object saga, Set<AssociationValue> associationValues)
      Description copied from interface: SagaStore
      Adds a new Saga and its initial association values to the store. The tracking token of the event last handled by the Saga (usually the event that started the Saga) is also passed as a parameter. Note that the given token may be null if the Saga is not tracking the event store.
      Specified by:
      insertSaga in interface SagaStore<Object>
      Parameters:
      sagaType - The type of the Saga
      sagaIdentifier - The identifier of the Saga
      saga - The Saga instance
      associationValues - The initial association values of the Saga
    • setUseExplicitFlush

      public void setUseExplicitFlush(boolean useExplicitFlush)
      Sets whether or not to do an explicit EntityManager.flush() after each data modifying operation on the backing storage. Default to true
      Parameters:
      useExplicitFlush - true to force flush, false otherwise.
    • createSagaEntry

      protected SagaEntry<?> createSagaEntry(Object saga, String sagaIdentifier, Serializer serializer)
      Intended for clients to override. Defaults to SagaEntry.
      Parameters:
      saga - The Saga instance
      sagaIdentifier - The identifier of the Saga
      serializer - The serializer to serialize to the SagaEntry.getSerializedSaga()
      Returns:
      An instanceof @SagaEntry
    • sagaEntryEntityName

      protected String sagaEntryEntityName()
      Intended for clients to override. Defaults to 'SagaEntry'.
      Returns:
      the name of the Jpa event entity
    • serializedObjectType

      protected Class<? extends SimpleSerializedObject<?>> serializedObjectType()
      Intended for clients to override. Defaults to SerialzedSaga.class
      Returns:
      the serialized object type of the Saga this SagaStore stores