Skip to main content

TransactionScope

Struct TransactionScope 

Source
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>

Source

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.

Source

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.

Source

pub async fn load_or_default<A: Aggregate>( &mut self, id: &str, ) -> Result<AggregateRoot<A>, EventStoreError>

Source

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.

Source

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.)

Source

pub async fn save<A: Aggregate>( &mut self, root: &mut AggregateRoot<A>, ) -> Result<(), EventStoreError>

Source

pub async fn append<E: EventData>( &mut self, stream_id: &str, category: &str, expected: ExpectedVersion, events: &[Event<E>], ) -> Result<i64, EventStoreError>

Source

pub fn tx(&mut self) -> &mut Transaction<'static, Postgres>

Source

pub fn take_committed_events(&mut self) -> Vec<RecordedEvent>

Source

pub async fn commit(self) -> Result<CommittedEvents, EventStoreError>

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more