pub struct Transaction<'conn> { /* private fields */ }Expand description
Represents an active database transaction
Transactions provide ACID guarantees for multi-statement operations. Following the rusqlite pattern:
- Transactions automatically roll back when dropped
- Must explicitly call
commit()to persist changes - This prevents accidentally forgetting to commit or rollback
§Examples
// Transaction with explicit commit
let mut tx = session.transaction()?;
tx.execute("CREATE (p:Person {name: 'Alice'})")?;
tx.execute("CREATE (p:Person {name: 'Bob'})")?;
tx.commit()?; // Changes are persisted
// Transaction that rolls back (dropped without commit)
{
let mut tx = session.transaction()?;
tx.execute("CREATE (p:Person {name: 'Charlie'})")?;
// tx is dropped here, changes are automatically rolled back
}Implementations§
Source§impl<'conn> Transaction<'conn>
impl<'conn> Transaction<'conn>
Sourcepub fn query(&mut self, query: &str) -> Result<QueryResult>
pub fn query(&mut self, query: &str) -> Result<QueryResult>
Sourcepub fn commit(self) -> Result<()>
pub fn commit(self) -> Result<()>
Commit the transaction
Persists all changes made within this transaction. After calling commit(), the transaction is consumed and cannot be used further.
§Examples
let mut tx = session.transaction()?;
tx.execute("CREATE (p:Person {name: 'Alice'})")?;
tx.commit()?; // Changes are now persistentSourcepub fn rollback(self) -> Result<()>
pub fn rollback(self) -> Result<()>
Rollback the transaction
Discards all changes made within this transaction. This is called automatically when the transaction is dropped, so explicit rollback is rarely needed.
§Examples
let mut tx = session.transaction()?;
tx.execute("CREATE (p:Person {name: 'Alice'})")?;
tx.rollback()?; // Explicit rollback (optional, automatic on drop)Sourcepub fn set_drop_behavior(&mut self, behavior: DropBehavior)
pub fn set_drop_behavior(&mut self, behavior: DropBehavior)
Set the behavior when this transaction is dropped
By default, transactions roll back when dropped. This can be changed to:
DropBehavior::Commit- Auto-commit on dropDropBehavior::Panic- Panic if dropped without explicit commit/rollbackDropBehavior::Ignore- Do nothing (dangerous)
§Examples
let mut tx = session.transaction()?;
tx.set_drop_behavior(DropBehavior::Panic);
tx.execute("CREATE (p:Person {name: 'Alice'})")?;
// Must explicitly commit or rollback, or will panic on drop
tx.commit()?;Trait Implementations§
Source§impl<'conn> Drop for Transaction<'conn>
impl<'conn> Drop for Transaction<'conn>
Auto Trait Implementations§
impl<'conn> Freeze for Transaction<'conn>
impl<'conn> RefUnwindSafe for Transaction<'conn>
impl<'conn> Send for Transaction<'conn>
impl<'conn> Sync for Transaction<'conn>
impl<'conn> Unpin for Transaction<'conn>
impl<'conn> UnsafeUnpin for Transaction<'conn>
impl<'conn> UnwindSafe for Transaction<'conn>
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