fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
fbcpp::Transaction Class Referencefinal

Represents a transaction in one or more Firebird databases. More...

#include <Transaction.h>

Public Member Functions

 Transaction (Attachment &attachment, const TransactionOptions &options={})
 Constructs a Transaction object that starts a transaction in the specified Attachment using the specified options.
 
 Transaction (Attachment &attachment, std::string_view setTransactionCmd)
 Constructs a Transaction object that starts a transaction specified by a SET TRANSACTION command.
 
 Transaction (std::span< std::reference_wrapper< Attachment > > attachments, const TransactionOptions &options={})
 Constructs a Transaction object that starts a multi-database transaction across the specified Attachments using the specified options.
 
 Transaction (Transaction &&o) noexcept
 Move constructor.
 
Transactionoperator= (Transaction &&)=delete
 
 Transaction (const Transaction &)=delete
 
Transactionoperator= (const Transaction &)=delete
 
 ~Transaction () noexcept
 Rolls back the transaction if it is still active.
 
bool isValid () noexcept
 Returns whether the Transaction object is valid.
 
FbRef< fb::ITransaction > getHandle () noexcept
 Returns the internal Firebird ITransaction handle.
 
TransactionState getState () const noexcept
 Returns the current transaction state.
 
void prepare ()
 Prepares the transaction for two-phase commit (2PC phase 1).
 
void prepare (std::string_view message)
 Prepares the transaction for two-phase commit with a text message identifier.
 
void prepare (std::span< const std::uint8_t > message)
 Prepares the transaction for two-phase commit with a binary message identifier.
 
void commit ()
 Commits the transaction.
 
void commitRetaining ()
 Commits the transaction while maintains it active.
 
void rollback ()
 Rolls back the transaction.
 
void rollbackRetaining ()
 Rolls back the transaction while maintains it active.
 

Detailed Description

Represents a transaction in one or more Firebird databases.

Single-database transactions are created using the Attachment constructor. Multi-database transactions are created using the vector of Attachments constructor and support two-phase commit (2PC) protocol via the prepare() method.

For 2PC:

  1. Create multi-database transaction with multiple Attachments
  2. Execute statements across databases
  3. Call prepare() to enter prepared state
  4. Call commit() or rollback() to complete

Important: Prepared transactions MUST be explicitly committed or rolled back. The destructor will NOT automatically rollback prepared transactions.

The Transaction must exist and remain valid while there are other objects using it, such as Statement. If a Transaction object is destroyed before being committed or rolled back (and not prepared), it will be automatically rolled back.

Definition at line 350 of file Transaction.h.

Constructor & Destructor Documentation

◆ Transaction() [1/4]

Transaction::Transaction ( Attachment attachment,
const TransactionOptions options = {} 
)
explicit

Constructs a Transaction object that starts a transaction in the specified Attachment using the specified options.

Definition at line 135 of file Transaction.cpp.

◆ Transaction() [2/4]

Transaction::Transaction ( Attachment attachment,
std::string_view  setTransactionCmd 
)
explicit

Constructs a Transaction object that starts a transaction specified by a SET TRANSACTION command.

Definition at line 152 of file Transaction.cpp.

◆ Transaction() [3/4]

Transaction::Transaction ( std::span< std::reference_wrapper< Attachment > >  attachments,
const TransactionOptions options = {} 
)
explicit

Constructs a Transaction object that starts a multi-database transaction across the specified Attachments using the specified options.

All attachments must use the same Client. The same TransactionOptions (TPB) will be applied to all databases.

This constructor enables two-phase commit (2PC) support via the prepare() method.

Definition at line 165 of file Transaction.cpp.

◆ Transaction() [4/4]

fbcpp::Transaction::Transaction ( Transaction &&  o)
inlinenoexcept

Move constructor.

A moved Transaction object becomes invalid.

Definition at line 381 of file Transaction.h.

◆ ~Transaction()

fbcpp::Transaction::~Transaction ( )
inlinenoexcept

Rolls back the transaction if it is still active.

Prepared transactions are NOT automatically rolled back and must be explicitly committed or rolled back before destruction.

Definition at line 401 of file Transaction.h.

Member Function Documentation

◆ commit()

void Transaction::commit ( )

Commits the transaction.

Can be called from ACTIVE or PREPARED state.

Definition at line 214 of file Transaction.cpp.

◆ commitRetaining()

void Transaction::commitRetaining ( )

Commits the transaction while maintains it active.

Cannot be called on a prepared transaction.

Definition at line 227 of file Transaction.cpp.

◆ getHandle()

FbRef< fb::ITransaction > fbcpp::Transaction::getHandle ( )
inlinenoexcept

Returns the internal Firebird ITransaction handle.

Definition at line 432 of file Transaction.h.

◆ getState()

TransactionState fbcpp::Transaction::getState ( ) const
inlinenoexcept

Returns the current transaction state.

Definition at line 440 of file Transaction.h.

◆ isValid()

bool fbcpp::Transaction::isValid ( )
inlinenoexcept

Returns whether the Transaction object is valid.

Definition at line 424 of file Transaction.h.

◆ prepare() [1/3]

void Transaction::prepare ( )

Prepares the transaction for two-phase commit (2PC phase 1).

After prepare() is called, the transaction must be explicitly committed or rolled back. The destructor will NOT automatically rollback prepared transactions.

Definition at line 249 of file Transaction.cpp.

◆ prepare() [2/3]

void Transaction::prepare ( std::span< const std::uint8_t >  message)

Prepares the transaction for two-phase commit with a binary message identifier.

The message can be used to identify the transaction during recovery operations.

Definition at line 254 of file Transaction.cpp.

◆ prepare() [3/3]

void Transaction::prepare ( std::string_view  message)

Prepares the transaction for two-phase commit with a text message identifier.

The message can be used to identify the transaction during recovery operations.

Definition at line 266 of file Transaction.cpp.

◆ rollback()

void Transaction::rollback ( )

Rolls back the transaction.

Can be called from ACTIVE or PREPARED state.

Definition at line 201 of file Transaction.cpp.

◆ rollbackRetaining()

void Transaction::rollbackRetaining ( )

Rolls back the transaction while maintains it active.

Cannot be called on a prepared transaction.

Definition at line 238 of file Transaction.cpp.


The documentation for this class was generated from the following files: