Saga
Persistence-agnostic contract for a saga aggregate.
Implementations can be backed by any storage technology (JPA, MongoDB, DynamoDB, in-memory, …). The engine and adapters only depend on this interface, never on a concrete entity class.
All fields are exposed as read-only (val). State transitions are performed exclusively through behavior methods that encapsulate the aggregate's invariants (DDD: rich aggregate methods, Vernon, IDDD).
Implementations decide internally how to realize the transition:
JPA-backed entities may mutate their
varfields and returnthis(relying on Hibernate dirty-tracking).Immutable documents (Mongo, in-memory) may return a fresh instance via
copy(...). The engine never observes this difference.
The interface is F-bounded (Saga<S : Saga<S>>) so behavior methods return the concrete adapter type S. This eliminates unchecked casts in the engine while preserving the rich-aggregate contract.
Inheritors
Properties
Instant the saga reached a terminal status; null while it is still in flight.
Current lifecycle SagaStatus; see the enum for the full state machine.
Saga type discriminator (e.g. "UserRegistration"). Conventionally a SagaTypeValue.
Functions
Transitions to SagaStatus.AWAITING_RESPONSE and stamps updatedAt.
Transitions to SagaStatus.COMPENSATED and stamps completedAt/updatedAt.
Transitions to SagaStatus.COMPENSATION_FAILED and stamps completedAt/updatedAt.
Transitions to SagaStatus.COMPLETED and stamps completedAt/updatedAt.
Transitions to SagaStatus.FAILED, stores error as lastError, and stamps completedAt/updatedAt.
Transitions to SagaStatus.COMPENSATING, stores error as lastError, and stamps updatedAt. Used when a step failure triggers compensation but the saga has not yet completed compensation.