Skip to main content

Module transaction

Module transaction 

Source
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? or ctx.transaction(...). Registers the handle as ambient; every statement issued through the context uses the transaction’s connection. Commits on Ok, rolls back on Err / TxOutcome::Rollback.
  • Manual — let txn = ctx.begin_transaction().await?; ... txn.commit().await?; Returns a standalone handle. It is not registered as ambient, so queries and save_changes() do not join it; use it for raw SQL and explicit savepoint/isolation control. Use ctx.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 ITransaction implementation.

Enums§

TxOutcome
The outcome of a DbContext::transaction closure.

Traits§

ITransaction
Transaction handle abstracting the Priority 2 connection-level transaction methods.