Transaction Class Reference

#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.

Protected Member Functions

virtual const char * onGetBeginSql ()
 Returns SQL for BEGIN, or a null pointer for the backend default.
virtual const char * onGetCommitSql ()
 Returns SQL for COMMIT, or a null pointer for the backend default.
virtual const char * onGetRollbackSql ()
 Returns SQL for ROLLBACK, or a null pointer for the backend default.

Detailed Description

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.

conn.execute("UPDATE t SET n = n + 1");
txn.commit();
Unit of work on a database connection.
Definition Transaction.h:74
Pt::Db::Transaction txn(conn, false);
co_await txn.beginAsync();
co_await conn.executeAsync("INSERT INTO t VALUES (1)");
co_await txn.commitAsync();

Constructor & Destructor Documentation

◆ Transaction()

Transaction ( Connection & conn,
bool starttransaction = true )

Begins the transaction unless starttransaction is false.

Member Function Documentation

◆ begin()

void begin ( )

Rolls back an already active transaction first.

◆ commit()

void commit ( )

Does nothing if no transaction is active.

◆ rollback()

void rollback ( )

Does nothing if no transaction is active.