Skip to main content

TransactionExecutor

Trait TransactionExecutor 

Source
pub trait TransactionExecutor: Send + Sync {
    // Required methods
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        sql: &'life1 str,
        params: Vec<QueryValue>,
    ) -> Pin<Box<dyn Future<Output = Result<QueryResult, DatabaseError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn fetch_one<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        sql: &'life1 str,
        params: Vec<QueryValue>,
    ) -> Pin<Box<dyn Future<Output = Result<Row, DatabaseError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn fetch_all<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        sql: &'life1 str,
        params: Vec<QueryValue>,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Row>, DatabaseError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn fetch_optional<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        sql: &'life1 str,
        params: Vec<QueryValue>,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Row>, DatabaseError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn commit<'async_trait>(
        self: Box<Self>,
    ) -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'async_trait>>
       where Self: 'async_trait;
    fn rollback<'async_trait>(
        self: Box<Self>,
    ) -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'async_trait>>
       where Self: 'async_trait;

    // Provided methods
    fn savepoint<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        name: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
    fn release_savepoint<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        name: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
    fn rollback_to_savepoint<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        name: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
}
Available on crate feature database and non-WebAssembly only.
Expand description

Transaction executor trait for database-specific transaction handling

This trait represents a dedicated database connection that is used for transaction operations. All queries executed through this executor are guaranteed to run on the same physical connection, ensuring proper transaction isolation.

§Implementation Notes

SQLx connection pools distribute queries across multiple connections. To ensure transaction consistency, we need to acquire a dedicated connection via pool.begin() which returns a Transaction that maintains connection affinity.

Required Methods§

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 mut self, sql: &'life1 str, params: Vec<QueryValue>, ) -> Pin<Box<dyn Future<Output = Result<QueryResult, DatabaseError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Execute a query that modifies the database within the transaction

Source

fn fetch_one<'life0, 'life1, 'async_trait>( &'life0 mut self, sql: &'life1 str, params: Vec<QueryValue>, ) -> Pin<Box<dyn Future<Output = Result<Row, DatabaseError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Fetch a single row within the transaction

Source

fn fetch_all<'life0, 'life1, 'async_trait>( &'life0 mut self, sql: &'life1 str, params: Vec<QueryValue>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Row>, DatabaseError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Fetch all matching rows within the transaction

Source

fn fetch_optional<'life0, 'life1, 'async_trait>( &'life0 mut self, sql: &'life1 str, params: Vec<QueryValue>, ) -> Pin<Box<dyn Future<Output = Result<Option<Row>, DatabaseError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Fetch an optional single row within the transaction

Source

fn commit<'async_trait>( self: Box<Self>, ) -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'async_trait>>
where Self: 'async_trait,

Commit the transaction

Source

fn rollback<'async_trait>( self: Box<Self>, ) -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'async_trait>>
where Self: 'async_trait,

Rollback the transaction

Provided Methods§

Source

fn savepoint<'life0, 'life1, 'async_trait>( &'life0 mut self, name: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Create a savepoint within the transaction

Savepoints allow creating checkpoints within a transaction that can be rolled back to without affecting the entire transaction.

§Arguments
  • name - The name of the savepoint to create
§Default Implementation

Returns an error indicating savepoints are not supported. Backends that support savepoints should override this method.

Source

fn release_savepoint<'life0, 'life1, 'async_trait>( &'life0 mut self, name: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Release (commit) a savepoint

Releasing a savepoint removes the checkpoint and makes the changes within it part of the enclosing transaction.

§Arguments
  • name - The name of the savepoint to release
§Default Implementation

Returns an error indicating savepoints are not supported.

Source

fn rollback_to_savepoint<'life0, 'life1, 'async_trait>( &'life0 mut self, name: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Rollback to a savepoint

Rolling back to a savepoint undoes all changes made after the savepoint was created, while keeping the transaction open.

§Arguments
  • name - The name of the savepoint to rollback to
§Default Implementation

Returns an error indicating savepoints are not supported.

Implementors§