Skip to content

Commit

Permalink
Add example on how to use Transaction as Executor (#3311)
Browse files Browse the repository at this point in the history
* add example using Transaction as Executor to docs

* change tx.as_mut() to &mut *tx
  • Loading branch information
Lachstec authored Jul 22, 2024
1 parent eaad7b2 commit 940d9fb
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions sqlx-core/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ pub trait TransactionManager {
/// executed after it was established to be rolled back, restoring the transaction state to
/// what it was at the time of the savepoint.
///
/// A transaction can be used as an [`Executor`] when performing queries:
/// ```rust,no_run
/// # use sqlx_core::acquire::Acquire;
/// # async fn example() -> sqlx::Result<()> {
/// # let id = 1;
/// # let mut conn: sqlx::PgConnection = unimplemented!();
/// let mut tx = conn.begin().await?;
///
/// let result = sqlx::query("DELETE FROM \"testcases\" WHERE id = $1")
/// .bind(id)
/// .execute(&mut *tx)
/// .await?
/// .rows_affected();
///
/// tx.commit().await
/// # }
/// ```
/// [`Executor`]: crate::executor::Executor
/// [`Connection::begin`]: crate::connection::Connection::begin()
/// [`Pool::begin`]: crate::pool::Pool::begin()
/// [`commit`]: Self::commit()
Expand Down

0 comments on commit 940d9fb

Please sign in to comment.