Skip to main content

Transaction

Struct Transaction 

Source
pub struct Transaction<'a> { /* private fields */ }
Expand description

A transaction guard. Ensures the transaction is committed or rolled back.

Created via PgConnection::transaction(). Provides the same query methods as PgConnection. On drop, if neither commit nor rollback was called, automatically rolls back.

Implementations§

Source§

impl<'a> Transaction<'a>

Source

pub fn commit(&mut self) -> PgResult<()>

Commit this transaction (or release savepoint if nested).

Source

pub fn rollback(&mut self) -> PgResult<()>

Rollback this transaction (or rollback to savepoint if nested).

Source

pub fn transaction<F, T>(&mut self, f: F) -> PgResult<T>
where F: FnOnce(&mut Transaction<'_>) -> PgResult<T>,

Execute a nested transaction using a SAVEPOINT.

Creates a savepoint, calls the closure, and either releases (on success) or rolls back to the savepoint (on error/drop).

Nesting is unlimited — each level creates a new savepoint.

§Example
conn.transaction(|tx| {
    tx.execute("INSERT INTO users (name) VALUES ($1)", &[&"Alice"])?;
    tx.transaction(|nested| {
        nested.execute("INSERT INTO logs (msg) VALUES ($1)", &[&"nested"])?;
        Ok(())
    })?;
    Ok(())
})?;
Source

pub fn query_simple(&mut self, sql: &str) -> PgResult<Vec<Row>>

Execute a simple query (no parameters).

Source

pub fn query(&mut self, sql: &str, params: &[&dyn ToSql]) -> PgResult<Vec<Row>>

Execute a parameterized query.

Source

pub fn query_one(&mut self, sql: &str, params: &[&dyn ToSql]) -> PgResult<Row>

Execute a query expecting exactly one row.

Source

pub fn query_opt( &mut self, sql: &str, params: &[&dyn ToSql], ) -> PgResult<Option<Row>>

Execute a query expecting zero or one row.

Source

pub fn execute(&mut self, sql: &str, params: &[&dyn ToSql]) -> PgResult<u64>

Execute a statement that returns no rows.

Source

pub fn savepoint(&mut self, name: &str) -> PgResult<()>

Create a savepoint within this transaction.

Source

pub fn rollback_to(&mut self, name: &str) -> PgResult<()>

Rollback to a savepoint.

Source

pub fn release_savepoint(&mut self, name: &str) -> PgResult<()>

Release a savepoint.

Source

pub fn status(&self) -> TransactionStatus

Get the transaction status.

Trait Implementations§

Source§

impl<'a> Drop for Transaction<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> UnsafeUnpin for Transaction<'a>

§

impl<'a> !UnwindSafe for Transaction<'a>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.