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 ConnectiongetConnection () const
 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::SignalstartFinished ()
 Signal emitted when an asynchronous begin completes.
void beginCommit ()
 Starts an asynchronous commit.
void endCommit ()
 Completes an asynchronous commit.
Pt::SignalcommitFinished ()
 Signal emitted when an asynchronous commit completes.
void beginRollback ()
 Starts an asynchronous rollback.
void endRollback ()
 Completes an asynchronous rollback.
Pt::SignalrollbackFinished ()
 Signal emitted when an asynchronous rollback completes.

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.

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:50

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.