CppDB
|
cppdb::session class has begin(), commit(), rollback() member functions that allow you to handle transactions. However you are not expected to use them directly for RAII reasons.
There is a transaction scope guard cppdb::transaction that allows to wrap transactions in exception safe way. In the transaction guard's constructor you specify the SQL session you want to run transaction on and when operations is completed you call its commit() function to complete the transaction or rollback()
If the transaction wasn't either committed or rolled back, it would be automatically rolled back during stack unwind.
For example:
cppdb::transaction guard(sql); sql << "UPDATE accounts SET amount=amount+? WHERE user=?" << amount << receiver << cppdb::exec; sql << "UPDATE accounts SET amount=amount-? WHERE user=?" << amount << sender << cppdb::exec; guard.commit();
This would execute a transaction in exception safe way.