Transactions let us update multiple documents in a batch. By using transactions, we can make sure that either the documents are all updated or none of them are updated.
let mut session = db.start_session().unwrap();
session.start_transaction(None).unwrap();
// ...
session.commit_transaction().unwrap();
We start a session first and then start a transaction.
All operations in //...
will be executed at commit_transaction().
Let us see an example.
use polodb_core::Database;
fn main() {
let db = Database::open_memory().unwrap();
let mut session = db.start_session().unwrap();
session.start_transaction(None).unwrap();
db.create_collection_with_session("name_of_the_collection", &mut session)
.unwrap();
let names = db.list_collection_names().unwrap();
println!("{:?}", names);
let names = db.list_collection_names_with_session(&mut session).unwrap();
println!("{:?}", names);
session.commit_transaction().unwrap();
let names = db.list_collection_names().unwrap();
println!("{:?}", names);
}
Output:
[]
["name_of_the_collection"]
["name_of_the_collection"]
We create a collection in the session. This does not change the database until we execute commit_transaction().
In addition to create_collection_with_session and list_collection_names_with_session, we can also use other methods that end with _with_session
, such as insert_many_with_session and update_many_with_session.
📘 Back: Table of contents