pub struct DistributedTransaction {
pub id: String,
pub participants: Vec<TransactionParticipant>,
pub state: TransactionState,
pub metadata: HashMap<String, String>,
}Expand description
Distributed transaction using Two-Phase Commit (2PC) protocol
Coordinates transactions across multiple databases to ensure atomicity.
§Example
use rust_logic_graph::multi_db::DistributedTransaction;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut txn = DistributedTransaction::new("order_txn_123");
// Register participants
txn.add_participant("orders_db", "Insert order");
txn.add_participant("inventory_db", "Decrement stock");
txn.add_participant("payments_db", "Charge customer");
// Phase 1: Prepare
txn.prepare().await?;
// Phase 2: Commit (or abort if any participant fails)
if txn.can_commit() {
txn.commit().await?;
} else {
txn.abort().await?;
}
Ok(())
}Fields§
§id: String§participants: Vec<TransactionParticipant>§state: TransactionState§metadata: HashMap<String, String>Implementations§
Source§impl DistributedTransaction
impl DistributedTransaction
Sourcepub fn add_participant(
&mut self,
database: impl Into<String>,
id: impl Into<String>,
) -> &mut Self
pub fn add_participant( &mut self, database: impl Into<String>, id: impl Into<String>, ) -> &mut Self
Add a participant to the transaction
Sourcepub fn add_metadata(
&mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> &mut Self
pub fn add_metadata( &mut self, key: impl Into<String>, value: impl Into<String>, ) -> &mut Self
Add metadata to the transaction
Sourcepub async fn prepare(&mut self) -> Result<bool, RustLogicGraphError>
pub async fn prepare(&mut self) -> Result<bool, RustLogicGraphError>
Phase 1: Prepare - Ask all participants if they can commit
In a real implementation, this would:
- Lock resources in each database
- Validate constraints
- Write to transaction log
- Return ready/abort status
Sourcepub fn can_commit(&self) -> bool
pub fn can_commit(&self) -> bool
Check if transaction can commit (all participants prepared)
Sourcepub async fn commit(&mut self) -> Result<(), RustLogicGraphError>
pub async fn commit(&mut self) -> Result<(), RustLogicGraphError>
Phase 2: Commit - Instruct all participants to commit
Sourcepub async fn abort(&mut self) -> Result<(), RustLogicGraphError>
pub async fn abort(&mut self) -> Result<(), RustLogicGraphError>
Abort/rollback the transaction
Trait Implementations§
Source§impl Clone for DistributedTransaction
impl Clone for DistributedTransaction
Source§fn clone(&self) -> DistributedTransaction
fn clone(&self) -> DistributedTransaction
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for DistributedTransaction
impl RefUnwindSafe for DistributedTransaction
impl Send for DistributedTransaction
impl Sync for DistributedTransaction
impl Unpin for DistributedTransaction
impl UnwindSafe for DistributedTransaction
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more