Skip to main content

with_transaction

Function with_transaction 

Source
pub async fn with_transaction<D, T, F>(ds: &D, work: F) -> Result<T, DataError>
where D: TransactionalDataSource + ?Sized, F: for<'t> FnOnce(&'t mut Box<dyn OutboxTx>) -> BoxFuture<'t, Result<T, DataError>>,
Expand description

Run work inside one transaction: commit on Ok, roll back on Err.

let order = with_transaction(ds, |tx| Box::pin(async move {
    let order = repo.insert_order(tx, &dto).await?;
    tx.enqueue(OutboxEntry { topic: "order.created".into() /* ... */ }).await?;
    Ok(order)
})).await?;