mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Add basic documentation for transactions
This commit is contained in:
parent
eb48909721
commit
027223c515
@ -20,5 +20,6 @@ The following pages will tell you how to use it:
|
|||||||
* [Remove](Remove.md)
|
* [Remove](Remove.md)
|
||||||
* [Functions](Functions.md)
|
* [Functions](Functions.md)
|
||||||
* [Prepared Statements](Prepared-Statements.md)
|
* [Prepared Statements](Prepared-Statements.md)
|
||||||
|
* [Transactions](Transactions.md)
|
||||||
* [NULL](NULL.md)
|
* [NULL](NULL.md)
|
||||||
* [New Features](New-Features.md)
|
* [New Features](New-Features.md)
|
||||||
|
44
docs/Transactions.md
Normal file
44
docs/Transactions.md
Normal file
@ -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);
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user