From 027223c5150b756144798f9234c135eefde5bcf3 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 8 Jan 2022 18:03:22 +0100 Subject: [PATCH] Add basic documentation for transactions --- docs/Home.md | 1 + docs/Transactions.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 docs/Transactions.md diff --git a/docs/Home.md b/docs/Home.md index bbaa4044..f897ccf0 100644 --- a/docs/Home.md +++ b/docs/Home.md @@ -20,5 +20,6 @@ The following pages will tell you how to use it: * [Remove](Remove.md) * [Functions](Functions.md) * [Prepared Statements](Prepared-Statements.md) +* [Transactions](Transactions.md) * [NULL](NULL.md) * [New Features](New-Features.md) diff --git a/docs/Transactions.md b/docs/Transactions.md new file mode 100644 index 00000000..76e74430 --- /dev/null +++ b/docs/Transactions.md @@ -0,0 +1,44 @@ +# Transactions + +Transactions are simple in sqlpp11. Assuming you have a connection called `db`, +you can start and commit a transaction just like this: + +```C++ +auto tx = start_transaction(db); +// do something +tx.commit(); +``` + +If you need to rollback the transaction, you can call it's `rollback()` member, +for instance like this: + +```C++ +auto tx = start_transaction(db); +try +{ + // do something + tx.commit(); +} +catch (...) +{ + tx.rollback(); +} +``` + +In case you call neither `commit()` nor `rollback()` on a transaction before it +goes out of scope, it will call `rollback()` in its destructor. This automatic +rollback will be reported by the connection. + +## More options +You can turn off reporting for automatic rollback in the destructor by passing +`sqlpp::quiet_auto_rollback`. + +```C++ +auto tx = start_transaction(db, sqlpp::quiet_auto_rollback); +``` + +You can also specify the isolation level for the transaction: + +```C++ +auto tx = start_transaction(db, ::sqlpp::isolation_level::repeatable_read); +```