Transaction

Struct Transaction 

Source
pub struct Transaction<'a, S: EventStore, C: ConcurrencyStrategy = Unchecked> { /* private fields */ }
Expand description

Transaction for appending events to an aggregate instance.

Events are accumulated in the transaction and persisted atomically when commit() is called. If the transaction is dropped without calling commit(), the events are silently discarded (rolled back). This allows errors during event serialization to be handled gracefully.

The C type parameter determines the concurrency strategy:

Implementations§

Source§

impl<'a, S: EventStore, C: ConcurrencyStrategy> Transaction<'a, S, C>

Source

pub const fn new( store: &'a mut S, aggregate_kind: String, aggregate_id: S::Id, expected_version: Option<S::Position>, ) -> Self

Source

pub fn append<E>( &mut self, event: E, metadata: S::Metadata, ) -> Result<(), <S::Codec as Codec>::Error>

Append an event to the transaction.

For sum-type events (enums), this serializes each variant to its persistable form.

§Errors

Returns a codec error if serialization fails.

Source§

impl<S: EventStore> Transaction<'_, S, Unchecked>

Source

pub async fn commit(self) -> Result<(), S::Error>

Commit the transaction without version checking.

Events are persisted atomically. No conflict detection is performed.

§Errors

Returns a store error if persistence fails.

Source§

impl<S: EventStore> Transaction<'_, S, Optimistic>

Source

pub async fn commit(self) -> Result<(), AppendError<S::Position, S::Error>>

Commit the transaction with version checking.

The commit will fail with a ConcurrencyConflict if the stream version has changed since the aggregate was loaded.

When expected_version is None, this means we expect a new aggregate (empty stream). The commit will fail with a conflict if the stream already has events.

§Errors

Returns AppendError::Conflict if another writer modified the stream, or AppendError::Store if persistence fails.

Trait Implementations§

Source§

impl<S: EventStore, C: ConcurrencyStrategy> Drop for Transaction<'_, S, C>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, S, C> Freeze for Transaction<'a, S, C>
where <S as EventStore>::Id: Freeze, <S as EventStore>::Position: Freeze,

§

impl<'a, S, C> RefUnwindSafe for Transaction<'a, S, C>

§

impl<'a, S, C> Send for Transaction<'a, S, C>

§

impl<'a, S, C> Sync for Transaction<'a, S, C>

§

impl<'a, S, C> Unpin for Transaction<'a, S, C>
where <S as EventStore>::Id: Unpin, <S as EventStore>::Position: Unpin, C: Unpin, <S as EventStore>::Metadata: Unpin,

§

impl<'a, S, C = Unchecked> !UnwindSafe for Transaction<'a, S, C>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

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
§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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