Skip to main content

hypercall_db/traits/
transaction.rs

1//! Transaction abstraction for atomic multi-write operations.
2//!
3//! Most trait methods manage their own connection (checkout → execute → return).
4//! When multiple writes must be atomic, callers use `begin_transaction()` to get
5//! a `Transaction` that routes all operations through a single database connection
6//! with explicit commit/rollback.
7//!
8//! Transaction is NOT Send/Sync — it holds a database connection and must be
9//! used within a single task. This is intentional: transactions are scoped
10//! operations, not shared state.
11
12use anyhow::Result;
13
14/// A scoped database transaction.
15/// Commits on `.commit()`, rolls back on drop if not committed.
16///
17/// Transaction does NOT implement the Reader/Writer traits directly because
18/// those require Send + Sync (for `Arc<dyn Trait>` usage). Instead, Transaction
19/// provides equivalent methods that execute against the held connection.
20/// Domain-specific methods are added as each write path is extracted.
21pub trait Transaction {
22    fn commit(self) -> Result<()>;
23    fn rollback(self) -> Result<()>;
24}
25
26/// Ability to start a transaction. Implemented by DatabaseHandler.
27pub trait Transactional: Send + Sync {
28    type Tx: Transaction;
29    fn begin_transaction(&self) -> Result<Self::Tx>;
30}