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>
impl<'a> Transaction<'a>
Sourcepub fn commit(&mut self) -> PgResult<()>
pub fn commit(&mut self) -> PgResult<()>
Commit this transaction (or release savepoint if nested).
Sourcepub fn rollback(&mut self) -> PgResult<()>
pub fn rollback(&mut self) -> PgResult<()>
Rollback this transaction (or rollback to savepoint if nested).
Sourcepub fn transaction<F, T>(&mut self, f: F) -> PgResult<T>
pub fn transaction<F, T>(&mut self, f: F) -> 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(())
})?;Sourcepub fn query_simple(&mut self, sql: &str) -> PgResult<Vec<Row>>
pub fn query_simple(&mut self, sql: &str) -> PgResult<Vec<Row>>
Execute a simple query (no parameters).
Sourcepub fn query(&mut self, sql: &str, params: &[&dyn ToSql]) -> PgResult<Vec<Row>>
pub fn query(&mut self, sql: &str, params: &[&dyn ToSql]) -> PgResult<Vec<Row>>
Execute a parameterized query.
Sourcepub fn query_one(&mut self, sql: &str, params: &[&dyn ToSql]) -> PgResult<Row>
pub fn query_one(&mut self, sql: &str, params: &[&dyn ToSql]) -> PgResult<Row>
Execute a query expecting exactly one row.
Sourcepub fn query_opt(
&mut self,
sql: &str,
params: &[&dyn ToSql],
) -> PgResult<Option<Row>>
pub fn query_opt( &mut self, sql: &str, params: &[&dyn ToSql], ) -> PgResult<Option<Row>>
Execute a query expecting zero or one row.
Sourcepub fn execute(&mut self, sql: &str, params: &[&dyn ToSql]) -> PgResult<u64>
pub fn execute(&mut self, sql: &str, params: &[&dyn ToSql]) -> PgResult<u64>
Execute a statement that returns no rows.
Sourcepub fn savepoint(&mut self, name: &str) -> PgResult<()>
pub fn savepoint(&mut self, name: &str) -> PgResult<()>
Create a savepoint within this transaction.
Sourcepub fn rollback_to(&mut self, name: &str) -> PgResult<()>
pub fn rollback_to(&mut self, name: &str) -> PgResult<()>
Rollback to a savepoint.
Sourcepub fn release_savepoint(&mut self, name: &str) -> PgResult<()>
pub fn release_savepoint(&mut self, name: &str) -> PgResult<()>
Release a savepoint.
Sourcepub fn status(&self) -> TransactionStatus
pub fn status(&self) -> TransactionStatus
Get the transaction status.