docs
Transactions

Transactions

A transaction symbolizes a unit of work. In PoloDB, an operation on a single document is atomic.

For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), PoloDB supports multi-document transactions.

Transactions API

This example highlights the key components of the transactions API. In particular, it uses the API with _session suffix. The callback API:

  • starts a transaction
  • executes the specified operations
  • commits the result (or aborts on error)
use polodb_core::Database;
use polodb_core::bson::{Document, doc};
let db = Database::open_file(db_path).unwrap();
 
let txn = db.start_transaction().unwrap();
 
// the collection is associated with the transaction
let collection = txn.collection::<Document>("books");
 
let docs = vec![
    doc! { "title": "1984", "author": "George Orwell" },
    doc! { "title": "Animal Farm", "author": "George Orwell" },
    doc! { "title": "The Great Gatsby", "author": "F. Scott Fitzgerald" },
];
collection.insert_many(docs).unwrap();
 
txn.commit().unwrap();