Skip to main content

Transaction

Struct Transaction 

Source
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>

Source

pub fn execute(&mut self, statement: &str) -> Result<()>

Execute a GQL statement within this transaction

§Arguments
  • statement - GQL statement to execute
§Examples
let mut tx = session.transaction()?;
tx.execute("CREATE (p:Person {name: 'Alice'})")?;
tx.execute("CREATE (p:Person {name: 'Bob'})")?;
tx.commit()?;
Source

pub fn query(&mut self, query: &str) -> Result<QueryResult>

Execute a query within this transaction and return results

§Arguments
  • query - GQL query to execute
§Examples
let mut tx = session.transaction()?;
tx.execute("CREATE (p:Person {name: 'Alice', age: 30})")?;
let result = tx.query("MATCH (p:Person) RETURN p")?;
tx.commit()?;
Source

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 persistent
Source

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)
Source

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 drop
  • DropBehavior::Panic - Panic if dropped without explicit commit/rollback
  • DropBehavior::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>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.