pub struct Transaction { /* private fields */ }Expand description
A database transaction.
Created by Pool::begin(). Must be explicitly
committed via commit(). If dropped without
commit(), the connection is discarded from the pool and a warning is logged.
Use .defer(&tx) on queries to buffer writes, then tx.commit() to flush
them all in a single pipeline. Use .run(&tx) or .fetch(&tx) for immediate
execution within the transaction.
§Example
use bsql::Pool;
let pool = Pool::connect("postgres://user:pass@localhost/mydb")?;
let tx = pool.begin()?;
// Buffer writes with .defer() — nothing hits the network yet
bsql::query!("INSERT INTO log (msg) VALUES ($msg: &str)")
.defer(&tx)?;
// Or execute immediately within the transaction
bsql::query!("UPDATE accounts SET balance = 0 WHERE id = $id: i32")
.run(&tx)?;
// commit() flushes all deferred operations, then commits
tx.commit()?;Implementations§
Source§impl Transaction
impl Transaction
Sourcepub async fn commit(self) -> BsqlResult<()>
pub async fn commit(self) -> BsqlResult<()>
Commit the transaction and return the connection to the pool.
Consumes self — the transaction cannot be used after commit.
Sourcepub async fn rollback(self) -> BsqlResult<()>
pub async fn rollback(self) -> BsqlResult<()>
Explicitly roll back the transaction and return the connection to the pool.
Consumes self — the transaction cannot be used after rollback.
Sourcepub async fn savepoint(&self, name: &str) -> BsqlResult<()>
pub async fn savepoint(&self, name: &str) -> BsqlResult<()>
Create a savepoint within the transaction.
The name must be a valid SQL identifier: ASCII alphanumeric and
underscores only, starting with a letter or underscore. Maximum 63 characters.
Sourcepub async fn release_savepoint(&self, name: &str) -> BsqlResult<()>
pub async fn release_savepoint(&self, name: &str) -> BsqlResult<()>
Release (destroy) a savepoint, keeping its effects.
The name must match a previously created savepoint.
Sourcepub async fn rollback_to(&self, name: &str) -> BsqlResult<()>
pub async fn rollback_to(&self, name: &str) -> BsqlResult<()>
Roll back to a savepoint, undoing changes made after it was created.
The savepoint remains valid after this call (can be rolled back to again).
Sourcepub async fn set_isolation(&self, level: IsolationLevel) -> BsqlResult<()>
pub async fn set_isolation(&self, level: IsolationLevel) -> BsqlResult<()>
Set the isolation level for this transaction.
Must be called before the first query in the transaction (immediately
after begin()). PostgreSQL rejects SET TRANSACTION after any
data-modifying statement.
Sourcepub async fn execute_pipeline(
&self,
sql: &str,
sql_hash: u64,
param_sets: &[&[&(dyn Encode + Sync)]],
) -> BsqlResult<Vec<u64>>
pub async fn execute_pipeline( &self, sql: &str, sql_hash: u64, param_sets: &[&[&(dyn Encode + Sync)]], ) -> BsqlResult<Vec<u64>>
Execute the same statement N times with different params in one pipeline.
Sends all N Bind+Execute messages + one Sync. One round-trip for N operations within the transaction. Returns the affected row count for each parameter set.
Sourcepub async fn for_each_raw<F>(
&self,
sql: &str,
sql_hash: u64,
params: &[&(dyn Encode + Sync)],
f: F,
) -> BsqlResult<()>
pub async fn for_each_raw<F>( &self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], f: F, ) -> BsqlResult<()>
Process each row directly from the wire buffer within this transaction.
Zero arena allocation — the closure receives a PgDataRow that reads
columns directly from the DataRow message bytes.
Trait Implementations§
Source§impl Debug for Transaction
impl Debug for Transaction
Source§impl Drop for Transaction
impl Drop for Transaction
Source§impl Executor for Transaction
Available on non-crate feature async only.
impl Executor for Transaction
async only.