dbixx::transaction
This is simple class that allows executing exception safe transactions.
class transaction{ public: transaction(session &sql); void commit(); void rollback(); ~transaction(); };
When the object is created, transaction begins. It is equivalent to:
sql<<"begin",exec();
If commit()
called, "COMMIT" sql statement is executed and
nothing is done on destruction. rollback()
does the same,
but the statement is "ROLLBACK".
If the object is destructed and commit()
or rollback()
hadn't called before, then rallback()
is automatically called.
For example:
void move_money(string from,string to, int amount) { dbixx::transaction tr(sql); int id1=get_id(from); sql<<"UPDATE users SET account=account+? " "WHERE id=?", amount, id1,exec(); int id2=get_id(to); sql<<"UPDATE users SET account=account-? " "WHERE id=?", amount, id2,exec(); tr.commit(); }
If get_id(to)
throws, then transaction is automatically rolled back.