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

public class JdbcSagaStore extends Object implements SagaStore<Object>
A SagaStore implementation that uses JDBC to store and find Saga instances.

Before using this store make sure the database contains an association value- and saga entry table. These can be constructed with SagaSqlSchema.sql_createTableAssocValueEntry(Connection) and SagaSqlSchema.sql_createTableSagaEntry(Connection) respectively. For convenience, these tables can be constructed through the createSchema() operation.

This store manages no transaction of its own. It takes a Connection from the ConnectionProvider it was given, so its changes commit together with whatever else the surrounding transaction covers. Give it the same provider instance the transaction manager was given, and use a provider that resolves the connection bound to the calling thread's transaction on every call, such as SpringDataSourceConnectionProvider. A store holding a provider that hands out an unbound connection writes outside the transaction entirely.

Such a provider finds the transaction by thread, so the store has to be called on the thread that began it. A TransactionManager declaring requiresSameThreadInvocations() arranges that, and the ones shipped for Spring and JPA do. A custom transaction manager that leaves that method at its default of false does not, and neither does handler code that moves work onto a thread of its own: the provider then finds no transaction and silently hands out a connection that commits on its own, which survives a rollback of the unit of work.

Because every operation here issues several statements, saga handling should run inside a transaction. Without one each statement commits on its own, so a failure part way through leaves a saga and its associations inconsistent rather than failing cleanly.

The saga type column holds the saga class name and is matched against it. Axon Framework 4 derived both through its Serializer, which for the default configuration also produced the class name, so an existing table reads and writes unchanged. An application that mapped its saga classes to some other type name, an XStream alias being the usual way, has rows this store cannot route to and must rewrite that column to the class name first.

The saga state itself is converted with the configured Converter. Axon Framework 4 defaulted to the XStreamSerializer unless the application configured another serializer, so the stored state of such an application is XStream XML, which a JSON-based converter cannot read: loading such a saga fails. Give this store an XStreamConverter if you are migrating such a scenario to read that state and to write it back in the same form, which also lets Axon Framework 4 nodes keep handling the same sagas during a rolling upgrade.

Since:
2.1
Author:
Allard Buijze, Kristian Rosenvold
  • Constructor Details

  • Method Details

    • builder

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

      The SagaSqlSchema is defaulted to an GenericSagaSqlSchema.

      The ConnectionProvider and Converter are hard requirements and as such should be provided.

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

      public <S> @Nullable 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>
      Type Parameters:
      S - the specific saga type returned
      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
    • 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, applying the association values added and removed since it was last stored.
      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 association values of the Saga, carrying the additions and removals to apply
    • 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.
      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
    • createSchema

      public void createSchema() throws SQLException
      Creates the SQL Schema required to store Sagas and their associations.
      Throws:
      SQLException - when an error occurs preparing of executing the required statements