pub struct Transaction { /* private fields */ }Expand description
A database transaction, from Db::begin.
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub async fn rollback(self) -> Result<(), DbError>
pub async fn rollback(self) -> Result<(), DbError>
Rolls the transaction back (dropping it uncommitted does the same).
Sourcepub async fn savepoint<T, F>(&mut self, work: F) -> Result<T>
pub async fn savepoint<T, F>(&mut self, work: F) -> Result<T>
Runs work inside a savepoint (a transaction inside the
transaction): when it returns Err, only what it did is undone and
the transaction goes on; when it returns Ok, its changes stay, to
be committed (or rolled back) with the rest. Savepoints nest.
let mut tx = db.begin().await?;
renox::db::sql("INSERT INTO orders (total) VALUES (?)").bind(18_000).execute(&mut tx).await?;
// Optional: a voucher that may be used up already. If it is, the order stays.
let voucher = tx
.savepoint(|tx| {
Box::pin(async move {
let used = renox::db::sql("UPDATE vouchers SET used = used + 1 WHERE code = ? AND used < max_uses")
.bind("KOPI10")
.execute(&mut *tx)
.await?;
renox::abort_if(used == 0, StatusCode::CONFLICT, "used up")?;
Ok(())
})
})
.await;
tx.commit().await?;On PostgreSQL a failed statement stops the whole transaction until it rolls back; inside a savepoint only the savepoint rolls back, so the transaction can go on after an expected failure (a unique violation you handle, say).
Trait Implementations§
Source§impl Debug for Transaction
impl Debug for Transaction
impl<'c> Executor<'c> for &'c mut Transaction
Auto Trait Implementations§
impl !RefUnwindSafe for Transaction
impl !UnwindSafe for Transaction
impl Freeze for Transaction
impl Send for Transaction
impl Sync for Transaction
impl Unpin for Transaction
impl UnsafeUnpin for Transaction
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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> ⓘ
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 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> ⓘ
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