pub struct TransactionScope<'a> { /* private fields */ }Expand description
Read-before-write escape hatch. In a strict event-sourced design,
commands read from projections (CQRS) and write to aggregates. The
TransactionScope exists for edge cases where that separation is
impractical: cross-aggregate invariants enforced in a single deployment,
recovery tooling, one-shot migrations. Prefer a projection-driven
design wherever you can — a long-lived TransactionScope on a hot
stream blocks other writers and couples your read path to the event
store’s transaction lifecycle.
If you find yourself reaching for load_for_update
in steady-state command code, that’s a signal the projection boundary is
in the wrong place.
Implementations§
Source§impl<'a> TransactionScope<'a>
impl<'a> TransactionScope<'a>
Sourcepub async fn load<A: Aggregate>(
&mut self,
id: &str,
) -> Result<Option<AggregateRoot<A>>, EventStoreError>
pub async fn load<A: Aggregate>( &mut self, id: &str, ) -> Result<Option<AggregateRoot<A>>, EventStoreError>
Load A’s aggregate within this transaction. Returns None if the
stream does not exist. Does not lock the stream row — concurrent
writers to this stream are still possible until save
takes its own FOR UPDATE.
Sourcepub async fn load_for_update<A: Aggregate>(
&mut self,
id: &str,
) -> Result<Option<AggregateRoot<A>>, EventStoreError>
pub async fn load_for_update<A: Aggregate>( &mut self, id: &str, ) -> Result<Option<AggregateRoot<A>>, EventStoreError>
Load A’s aggregate within this transaction and take a FOR UPDATE
row-lock on its stream entry. Returns None if the stream does not
exist; in that case no lock is acquired (there is no row to lock),
and the standard optimistic-concurrency check at save time still
guards the create path.
Use this when a different stream’s write depends on this aggregate’s state and must not race a concurrent writer to this stream. Without the lock, you observe a snapshot that can be invalidated before commit.
Hot-path warning: holds the row lock until the transaction commits or rolls back. Long-running scopes block other writers to the same stream.
pub async fn load_or_default<A: Aggregate>( &mut self, id: &str, ) -> Result<AggregateRoot<A>, EventStoreError>
Sourcepub async fn load_or_default_for_update<A: Aggregate>(
&mut self,
id: &str,
) -> Result<AggregateRoot<A>, EventStoreError>
pub async fn load_or_default_for_update<A: Aggregate>( &mut self, id: &str, ) -> Result<AggregateRoot<A>, EventStoreError>
Like load_or_default but takes
FOR UPDATE on the stream row when one exists. See
load_for_update for caveats.
Sourcepub async fn load_snapshotted<A: Snapshot>(
&mut self,
id: &str,
) -> Result<Option<AggregateRoot<A>>, EventStoreError>
pub async fn load_snapshotted<A: Snapshot>( &mut self, id: &str, ) -> Result<Option<AggregateRoot<A>>, EventStoreError>
Snapshot-aware load within the transaction. Mirrors
EventStore::load_snapshotted, reading the snapshot and tail
through the scope’s transaction.
Consistency caveat: a default sqlx transaction runs at READ
COMMITTED, where each statement takes a fresh snapshot — so the
stream-version, snapshot, and tail reads here do not observe a
single point-in-time view, and a concurrent writer between them can
yield a (state, version) pair where state reflects more events
than version. The optimistic-concurrency check at save time still
prevents log corruption, but a cross-stream decision made on the
returned pair can be inconsistent. For a true point-in-time read,
take load_for_update (locks the stream row) or run the scope at
REPEATABLE READ. (Review CORE-9/SNAP-3.)
pub async fn save<A: Aggregate>( &mut self, root: &mut AggregateRoot<A>, ) -> Result<(), EventStoreError>
pub async fn append<E: EventData>( &mut self, stream_id: &str, category: &str, expected: ExpectedVersion, events: &[Event<E>], ) -> Result<i64, EventStoreError>
pub fn tx(&mut self) -> &mut Transaction<'static, Postgres>
pub fn take_committed_events(&mut self) -> Vec<RecordedEvent>
pub async fn commit(self) -> Result<CommittedEvents, EventStoreError>
pub async fn rollback(self) -> Result<(), EventStoreError>
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for TransactionScope<'a>
impl<'a> !UnwindSafe for TransactionScope<'a>
impl<'a> Freeze for TransactionScope<'a>
impl<'a> Send for TransactionScope<'a>
impl<'a> Sync for TransactionScope<'a>
impl<'a> Unpin for TransactionScope<'a>
impl<'a> UnsafeUnpin for TransactionScope<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more