pub struct Transaction<'a> { /* private fields */ }Expand description
A wrapper around a SQLx transaction.
Provides a way to execute multiple queries atomically. If any query fails, the transaction can be rolled back. If all succeed, it can be committed.
§Type Parameters
'a- The lifetime of the database connection source
§Fields
tx- The underlying SQLx transactiondriver- The database driver type (for query syntax handling)
Implementations§
Source§impl<'a> Transaction<'a>
impl<'a> Transaction<'a>
Sourcepub fn model<T: Model + Send + Sync + Unpin>(
&mut self,
) -> QueryBuilder<'a, T, &mut Transaction<'a, Any>>
pub fn model<T: Model + Send + Sync + Unpin>( &mut self, ) -> QueryBuilder<'a, T, &mut Transaction<'a, Any>>
Starts building a query within this transaction.
This method creates a new QueryBuilder that will execute its queries
as part of this transaction.
§Type Parameters
T- The Model type to query.
§Returns
A new QueryBuilder instance bound to this transaction.
§Example
let mut tx = db.begin().await?;
// These operations are part of the transaction
tx.model::<User>().insert(&user).await?;
tx.model::<Post>().insert(&post).await?;
tx.commit().await?;Sourcepub async fn commit(self) -> Result<(), Error>
pub async fn commit(self) -> Result<(), Error>
Commits the transaction.
Persists all changes made during the transaction to the database.
This consumes the Transaction instance.
§Returns
Ok(())- Transaction committed successfullyErr(sqlx::Error)- Database error during commit
Sourcepub async fn rollback(self) -> Result<(), Error>
pub async fn rollback(self) -> Result<(), Error>
Rolls back the transaction.
Reverts all changes made during the transaction. This happens automatically
if the Transaction is dropped without being committed, but this method
allows for explicit rollback.
§Returns
Ok(())- Transaction rolled back successfullyErr(sqlx::Error)- Database error during rollback
Trait Implementations§
Source§impl<'a, 'b> Connection for &'a mut Transaction<'b>
Implementation of Connection for a mutable reference to Transaction.
impl<'a, 'b> Connection for &'a mut Transaction<'b>
Implementation of Connection for a mutable reference to Transaction.
Source§impl<'a> Connection for Transaction<'a>
Implementation of Connection for a Transaction.
impl<'a> Connection for Transaction<'a>
Implementation of Connection for a Transaction.
Allows the QueryBuilder to use a transaction for executing queries.
Supports generic borrow lifetimes to allow multiple operations within
the same transaction scope.
Auto Trait Implementations§
impl<'a> Freeze for Transaction<'a>
impl<'a> !RefUnwindSafe for Transaction<'a>
impl<'a> Send for Transaction<'a>
impl<'a> !Sync for Transaction<'a>
impl<'a> Unpin for Transaction<'a>
impl<'a> !UnwindSafe for Transaction<'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