pub trait DatabaseExt {
// Required method
fn with_transaction<F, T>(&self, f: F) -> Result<T>
where F: FnMut(&Connection) -> Result<T, Error>;
// Provided method
fn with_transaction_opts<F, T>(
&self,
_opts: TransactionOptions,
f: F,
) -> Result<T>
where F: FnMut(&Connection) -> Result<T, Error> { ... }
}Available on crate feature
sqlite only.Expand description
Extension trait for database transaction support.
Provides closure-based transaction management with automatic commit/rollback.
Note: This is a simplified transaction API. For complex transactions spanning
multiple repositories, use the raw connection approach via SqliteDatabase::conn().
§Example
ⓘ
use stateset_db::{SqliteDatabase, DatabaseExt, TransactionOptions};
let db = SqliteDatabase::in_memory()?;
// Simple transaction using raw SQL
db.with_transaction(|conn| {
conn.execute("UPDATE inventory_balances SET quantity_on_hand = 100 WHERE item_id = 1", [])?;
conn.execute("INSERT INTO inventory_transactions (...) VALUES (...)", [...])?;
Ok(())
})?;
// Transaction with options
db.with_transaction_opts(
TransactionOptions::new().with_retries(3),
|conn| {
conn.execute("UPDATE orders SET status = 'completed' WHERE id = ?", [&order_id])?;
Ok(())
},
)?;Required Methods§
Sourcefn with_transaction<F, T>(&self, f: F) -> Result<T>
fn with_transaction<F, T>(&self, f: F) -> Result<T>
Execute a closure within a database transaction.
The transaction is automatically committed if the closure returns Ok,
and rolled back if it returns Err or panics.
Provided Methods§
Sourcefn with_transaction_opts<F, T>(
&self,
_opts: TransactionOptions,
f: F,
) -> Result<T>
fn with_transaction_opts<F, T>( &self, _opts: TransactionOptions, f: F, ) -> Result<T>
Execute a closure within a database transaction with custom options.
This method allows setting transaction options like timeout and retry behavior.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".