Skip to main content

Transaction

Struct Transaction 

Source
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(&mut tx) on queries to buffer writes, then tx.commit() to flush them all in a single pipeline. Use .run(&mut tx) or .fetch(&mut 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(&mut tx)?;

// Or execute immediately within the transaction
bsql::query!("UPDATE accounts SET balance = 0 WHERE id = $id: i32")
    .run(&mut tx)?;

// commit() flushes all deferred operations, then commits
tx.commit()?;

Implementations§

Source§

impl Transaction

Source

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.

Source

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.

Source

pub async fn savepoint(&mut 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.

Source

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

Release (destroy) a savepoint, keeping its effects.

The name must match a previously created savepoint.

Source

pub async fn rollback_to(&mut 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).

Source

pub async fn set_isolation(&mut 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.

Source

pub async fn execute_pipeline( &mut 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.

Source

pub async fn for_each_raw<F>( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], f: F, ) -> BsqlResult<()>
where F: FnMut(PgDataRow<'_>) -> 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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Transaction

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a> From<&'a mut Transaction> for QueryTarget<'a>

Source§

fn from(tx: &'a mut Transaction) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> Same for T

Source§

type Output = T

Should always be Self
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.