Skip to main content

TransactionOps

Trait TransactionOps 

Source
pub trait TransactionOps: Send {
    // Required methods
    fn query(
        &self,
        cx: &Cx,
        sql: &str,
        params: &[Value],
    ) -> impl Future<Output = Outcome<Vec<Row>, Error>> + Send;
    fn query_one(
        &self,
        cx: &Cx,
        sql: &str,
        params: &[Value],
    ) -> impl Future<Output = Outcome<Option<Row>, Error>> + Send;
    fn execute(
        &self,
        cx: &Cx,
        sql: &str,
        params: &[Value],
    ) -> impl Future<Output = Outcome<u64, Error>> + Send;
    fn savepoint(
        &self,
        cx: &Cx,
        name: &str,
    ) -> impl Future<Output = Outcome<(), Error>> + Send;
    fn rollback_to(
        &self,
        cx: &Cx,
        name: &str,
    ) -> impl Future<Output = Outcome<(), Error>> + Send;
    fn release(
        &self,
        cx: &Cx,
        name: &str,
    ) -> impl Future<Output = Outcome<(), Error>> + Send;
    fn commit(self, cx: &Cx) -> impl Future<Output = Outcome<(), Error>> + Send;
    fn rollback(
        self,
        cx: &Cx,
    ) -> impl Future<Output = Outcome<(), Error>> + Send;
}
Expand description

Trait for transaction operations.

This trait defines the interface for database transactions with support for savepoints. Transactions must be explicitly committed or rolled back; dropping without commit triggers automatic rollback.

§Savepoints

Savepoints allow partial rollback within a transaction:

let mut tx = conn.begin(&cx).await?;
tx.execute(&cx, "INSERT INTO t1 (a) VALUES (1)", &[]).await?;
tx.savepoint(&cx, "sp1").await?;
tx.execute(&cx, "INSERT INTO t1 (a) VALUES (2)", &[]).await?;
tx.rollback_to(&cx, "sp1").await?;  // Rollback only the second insert
tx.commit(&cx).await?;  // Only first insert is committed

Required Methods§

Source

fn query( &self, cx: &Cx, sql: &str, params: &[Value], ) -> impl Future<Output = Outcome<Vec<Row>, Error>> + Send

Execute a query within this transaction.

Source

fn query_one( &self, cx: &Cx, sql: &str, params: &[Value], ) -> impl Future<Output = Outcome<Option<Row>, Error>> + Send

Execute a query and return the first row, if any.

Source

fn execute( &self, cx: &Cx, sql: &str, params: &[Value], ) -> impl Future<Output = Outcome<u64, Error>> + Send

Execute a statement within this transaction.

Source

fn savepoint( &self, cx: &Cx, name: &str, ) -> impl Future<Output = Outcome<(), Error>> + Send

Create a savepoint within this transaction.

Savepoints allow partial rollback without aborting the entire transaction.

Source

fn rollback_to( &self, cx: &Cx, name: &str, ) -> impl Future<Output = Outcome<(), Error>> + Send

Rollback to a previously created savepoint.

All changes made after the savepoint are discarded, but the transaction remains active and changes before the savepoint are preserved.

Source

fn release( &self, cx: &Cx, name: &str, ) -> impl Future<Output = Outcome<(), Error>> + Send

Release a savepoint, making the changes permanent within the transaction.

This frees resources associated with the savepoint but does not commit changes to the database (that happens when the transaction commits).

Source

fn commit(self, cx: &Cx) -> impl Future<Output = Outcome<(), Error>> + Send

Commit the transaction, making all changes permanent.

Source

fn rollback(self, cx: &Cx) -> impl Future<Output = Outcome<(), Error>> + Send

Rollback the transaction, discarding all changes.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§