#include <Pt/Db/Transaction.h>
Unit of work on a database connection. More...
Inherits NonCopyable.
Inherited by SqliteTransaction.
Public Member Functions | |
| Transaction (Connection &conn, bool starttransaction=true) | |
| Creates a transaction on conn. | |
| ~Transaction () | |
| Rolls back an active transaction and destroys the object. | |
| const Connection & | getConnection () const |
| Returns the connection this transaction uses. | |
| Connection & | getConnection () |
| Returns the connection this transaction uses. | |
| void | begin () |
| Begins a deferred transaction. | |
| void | commit () |
| Commits the active transaction. | |
| void | rollback () |
| Rolls back the active transaction. | |
| void | beginStart () |
| Starts an asynchronous begin-transaction. | |
| void | endStart () |
| Completes an asynchronous begin-transaction. | |
| Pt::Signal & | startFinished () |
| Signal emitted when an asynchronous begin completes. | |
| void | beginCommit () |
| Starts an asynchronous commit. | |
| void | endCommit () |
| Completes an asynchronous commit. | |
| Pt::Signal & | commitFinished () |
| Signal emitted when an asynchronous commit completes. | |
| void | beginRollback () |
| Starts an asynchronous rollback. | |
| void | endRollback () |
| Completes an asynchronous rollback. | |
| Pt::Signal & | rollbackFinished () |
| Signal emitted when an asynchronous rollback completes. | |
| AsyncBegin | beginAsync () |
| Returns an awaitable that begins the transaction. | |
| AsyncCommit | commitAsync () |
| Returns an awaitable that commits the transaction. | |
| AsyncRollback | rollbackAsync () |
| Returns an awaitable that rolls back the transaction. | |
Transaction is the unit of work the group described. The constructor begins a deferred transaction unless starttransaction is false. The object is not copyable and does not own the connection.
begin() starts a transaction; if one is already active it is rolled back first. commit() keeps the changes. rollback() discards them. If the object is destroyed while still active, the destructor rolls back and swallows any error from that rollback.
Asynchronous begin, commit and rollback need setActive() on the connection. beginStart() / endStart() / startFinished() begin the transaction; endStart() is the call that marks it active. beginCommit() / endCommit() / commitFinished() commit. beginRollback() / endRollback() / rollbackFinished() roll back.
When C++20 is available, beginAsync(), commitAsync() and rollbackAsync() wrap those pairs for co_await. Only one awaitable may be pending on the connection. Destroying the transaction while one is in flight detaches it; a later resume throws std::logic_error.
Override onGetBeginSql(), onGetCommitSql() and onGetRollbackSql() to supply backend-specific SQL. A null return lets the backend use its default.
| Transaction | ( | Connection & | conn, |
| bool | starttransaction = true ) |
Begins the transaction unless starttransaction is false.
| void begin | ( | ) |
Rolls back an already active transaction first.
| void commit | ( | ) |
Does nothing if no transaction is active.
| void rollback | ( | ) |
Does nothing if no transaction is active.