DMSCDatabaseTransaction

Trait DMSCDatabaseTransaction 

Source
pub trait DMSCDatabaseTransaction: Send + Sync {
    // Required methods
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<u64>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn query<'life0, 'life1, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<DMSCDBResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn commit<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn rollback<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn close<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

Trait representing a database transaction.

Transactions provide atomic operations where multiple SQL statements can be executed together and either committed or rolled back as a unit. This ensures data integrity even when operations fail partway through.

§Transaction Lifecycle

  1. Begin: Transaction starts with DMSCDatabase::transaction()
  2. Execute: Run SQL statements within the transaction
  3. Commit: Save all changes with commit() OR Rollback: Discard all changes with rollback()

§Example

use dmsc::database::{DMSCDatabasePool, DMSCDatabaseConfig};

#[tokio::main]
async fn main() -> DMSCResult<()> {
    let config = DMSCDatabaseConfig::postgres()
        .host("localhost")
        .database("mydb")
        .build();

    let pool = DMSCDatabasePool::new(config).await?;
    let db = pool.get().await?;

    let mut tx = db.transaction().await?;

    // Transfer funds between accounts
    tx.execute("UPDATE accounts SET balance = balance - 100 WHERE id = $1", &[&1]).await?;
    tx.execute("UPDATE accounts SET balance = balance + 100 WHERE id = $1", &[&2]).await?;

    tx.commit().await?;
    Ok(())
}

§Auto-Rollback

If a transaction is dropped without explicitly calling commit() or rollback(), it will automatically be rolled back to ensure no partial changes are persisted.

Required Methods§

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, ) -> Pin<Box<dyn Future<Output = DMSCResult<u64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Executes a SQL statement within the transaction.

See DMSCDatabase::execute() for more details.

Source

fn query<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, ) -> Pin<Box<dyn Future<Output = DMSCResult<DMSCDBResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Executes a SQL query within the transaction.

See DMSCDatabase::query() for more details.

Source

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

Commits all changes made within the transaction.

After calling this method, the transaction is complete and all changes are permanent.

§Errors

Returns an error if the commit fails. In this case, the transaction should be considered failed and no further operations should be attempted.

Source

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

Rolls back all changes made within the transaction.

This discards all changes made since the transaction began. The transaction is then complete and cannot be used further.

§Errors

Returns an error if the rollback fails. In this case, the transaction state is uncertain and the database should be checked for consistency.

Source

fn close<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = DMSCResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Closes the transaction without committing.

If the transaction has not been committed, this will implicitly roll back any changes. This method is primarily for cleanup and should not be used as a substitute for explicit rollback.

Implementors§