Expand description
Transaction handle abstraction.
Decouples transaction capabilities from DbContext so callers can hold a
typed handle (Box<dyn ITransaction>) returned by
DbContext::begin_transaction(). The handle exposes commit / rollback
(which consume it), savepoint operations, isolation level control, and a
connection() accessor used internally by save_changes() to reuse the
transaction’s connection.
§Control modes
- Scoped —
ctx.use_transaction(|ctx| Box::pin(async move { ... })).await?orctx.transaction(...). Registers the handle as ambient; every statement issued through the context uses the transaction’s connection. Commits onOk, rolls back onErr/TxOutcome::Rollback. - Manual —
let txn = ctx.begin_transaction().await?; ... txn.commit().await?;Returns a standalone handle. It is not registered as ambient, so queries andsave_changes()do not join it; use it for raw SQL and explicit savepoint/isolation control. Usectx.transaction(...)when the whole context should join the transaction.
Any transaction handle that is dropped without commit / rollback is
rolled back on a background task, matching the Dispose-rolls-back
convention of EF Core / ADO.NET. The rollback is best effort: it requires a
Tokio runtime to be available when the handle is dropped.
Structs§
- DbTransaction
- Default
ITransactionimplementation.
Enums§
- TxOutcome
- The outcome of a
DbContext::transactionclosure.
Traits§
- ITransaction
- Transaction handle abstracting the Priority 2 connection-level transaction methods.