Skip to main content

Module transaction

Module transaction 

Source
Expand description

Transaction handle abstraction.

DbContext::begin_transaction() returns a context-scoped ITransaction. The handle exposes commit / rollback (which consume it), savepoint operations, isolation control, and a connection() accessor used internally by save_changes().

§Control modes

  • Manual — let txn = ctx.begin_transaction().await?; ... txn.commit().await?; The transaction is registered on the context: queries, bulk DML, raw SQL and save_changes() all use its connection. Commit or rollback explicitly.
  • Closure — ctx.use_transaction(|ctx| Box::pin(async move { ... })).await?. Commits on Ok, rolls back on Err.

Any handle dropped without commit / rollback is rolled back on a background task, matching EF Core / ADO.NET Dispose semantics. The runtime is captured when the handle is created, so dropping it outside a Tokio context can still schedule the rollback. The rollback is best effort: it cannot run if the runtime is already shut down, and panic = "abort" skips Drop entirely.

Structs§

DbTransaction
Default ITransaction implementation for context-scoped transactions.

Traits§

ITransaction
Transaction handle abstracting the connection-level transaction methods.