Wt: Wt::Dbo::Transaction Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Wt::Dbo::Transaction Class Reference
Database Objects (Wt::Dbo)

A database transaction. More...

`#include <Wt/Dbo/Transaction.h>`

## Public Member Functions
| Transaction (Session &session)
| Constructor.
virtual | ~Transaction () noexcept(false)
| Destructor.
bool | isActive () const
| Returns whether the transaction is still active.
bool | commit ()
| Commits the transaction.
void | rollback ()
| Rolls back the transaction.
Session & | session () const
| Returns the session associated with this transaction.
SqlConnection * | connection () const
| Returns the connection used by this transaction.

## Detailed Description

A database transaction.

This class implements a RAII transaction. Most dbo manipulations require that a transaction is active, and database changes will not be committed to the database until the active transaction is committed. A transaction can be committed using commit(), but also commits automatically when it is deleted while no exception is being thrown (using std::uncaught_exception()). This means that it is possible that the transaction destructor throws an exception in case the transaction still needs to be committed, and the commit fails. If the transaction is deleted because of stack unwinding while an exception is thrown, then the transaction rolls back.

A transaction is active until it is either committed or rolled back. When a transaction is rolled back or fails, the modified database objects are not successfully synchronized with the database. A roll-back does not affect the value of the in memory database objects, so they may possibly be synchronized later in a new transaction or discarded using Session::rereadAll().

In most occasions you will want to guard any method that touches the database using a transaction object on the stack.

But you may create multiple (nested) transaction objects at the same time: in this way you can guard a method with a transaction object even if it is called from another method which also defines a transaction at a wider scope. Nested transactions act in concert and reference the same logical transaction: the logical transaction will fail if at least one transaction fails, and will be committed only if all transactions are committed.

Usage example:

void doSomething(Wt::Dbo::Session& session)
{
Wt::Dbo::Transaction transaction(session);
Wt::Dbo::ptr<Account> a = session.load<Account>(42);
...
// the transaction will roll back if an exception is thrown, or
// commit otherwise
}
Wt::Dbo::Session
A database session.
Definition Session.h:124
Wt::Dbo::Session::load
ptr< C > load(const typename dbo_traits< C >::IdType &id, bool forceReread=false)
Loads a persisted object.
Definition Session_impl.h:236
Wt::Dbo::Transaction
A database transaction.
Definition Transaction.h:71
Wt::Dbo::Transaction::session
Session & session() const
Returns the session associated with this transaction.
Wt::Dbo::ptr
A smart pointer for a database object.
Definition WGlobal.h:283

## Constructor & Destructor Documentation

## ◆ Transaction()

Wt::Dbo::Transaction::Transaction | ( | Session & | session | ) |
| explicit

Constructor.

Opens a transaction for the given `session`. If a transaction is already open for the session, this transaction is added. All open transactions must commit successfully for the entire transaction to succeed.

## ◆ ~Transaction()

virtual Wt::Dbo::Transaction::~Transaction | ( |  | ) |
| virtual

Destructor.

Under normal circumstances, the destructor will attempt to commit the transaction if it is active. If there's an active exception, or the last commit() threw, the transaction will be rolled back.

## Member Function Documentation

## ◆ commit()

bool Wt::Dbo::Transaction::commit | ( |  | ) |

Commits the transaction.

If this is the last open transaction for the session, the session is flushed and pending changes are committed to the database.

Returns whether the transaction was flushed to the database (i.e. whether this was indeed the last open transaction).

See also
  rollback()

## ◆ isActive()

bool Wt::Dbo::Transaction::isActive | ( |  | ) | const

Returns whether the transaction is still active.

A transaction is active unless it has been committed or rolled back.

While a transaction is active, new transactions for the same session are treated as nested transactions.

## ◆ rollback()

void Wt::Dbo::Transaction::rollback | ( |  | ) |

Rolls back the transaction.

The transaction is rolled back (if it was still active), and is no longer active.

See also
  commit()

## ◆ session()

Session & Wt::Dbo::Transaction::session | ( |  | ) | const

Returns the session associated with this transaction.

See also
  Transaction()