Transactions

Begin, commit and roll back a unit of work. More...

Classes

class  Transaction
 Unit of work on a database connection. More...
class  SqliteTransaction
 SQLite transaction with optional immediate locking. More...

Detailed Description

Transaction is a unit of work on one Connection. The constructor begins a deferred transaction unless the start flag is false. begin() starts one; if a transaction is already active it is rolled back first. commit() ends it and keeps the changes. rollback() ends it and discards them. If the object is destroyed while still active, the destructor rolls back and swallows any error from that rollback.

The object is not copyable. It does not own the connection.

Asynchronous begin, commit and rollback follow the connection model and need setActive() on that connection. beginStart() / endStart() / startFinished() begin the transaction. beginCommit() / endCommit() / commitFinished() commit it. beginRollback() / endRollback() / rollbackFinished() roll it back. endStart() is the call that marks the transaction active on the async path.

Override onGetBeginSql(), onGetCommitSql() and onGetRollbackSql() to supply backend-specific SQL. A null return lets the backend use its default. SqliteTransaction() uses that hook to run BEGIN IMMEDIATE TRANSACTION when immediate locking is requested.

conn.execute("UPDATE accounts SET balance = balance - 10 WHERE id = 1");
conn.execute("UPDATE accounts SET balance = balance + 10 WHERE id = 2");
txn.commit();
Unit of work on a database connection.
Definition Transaction.h:50