ObjectBox C and C++ API  4.0.3
obx::Transaction Class Reference

Provides RAII wrapper for an active database transaction on the current thread (do not use across threads). A Transaction object is considered a "top level transaction" if it is the first one on the call stack in the thread. If the thread already has an ongoing Transaction, additional Transaction instances are considered "inner transactions". More...

#include <objectbox.hpp>

Public Member Functions

 Transaction (Store &store, TxMode mode)
 
 Transaction (const Transaction &)=delete
 Delete because the default copy constructor can break things (i.e. a Transaction can not be copied). More...
 
 Transaction (Transaction &&source) noexcept
 Move constructor, used by Store::tx() More...
 
Transactionoperator= (Transaction source)
 Copy-and-swap style. More...
 
virtual ~Transaction ()
 Never throws. More...
 
bool isActive ()
 A Transaction is active if it was not ended via success(), close() or moving. More...
 
OBX_txncPtr () const
 The transaction pointer of the ObjectBox C API. More...
 
void success ()
 "Finishes" this write transaction successfully; performs a commit if this is the top level transaction and all inner transactions (if any) were also successful. This object will also be "closed". More...
 
obx_err closeNoThrow ()
 Explicit close to free up resources (non-throwing version). It's OK to call this method multiple times; additional calls will have no effect. More...
 
void close ()
 Explicit close to free up resources; unlike closeNoThrow() (which is also called by the destructor), this version throw in the unlikely case of failing. It's OK to call this method multiple times; additional calls will have no effect. More...
 
uint64_t getDataSizeCommitted () const
 The data size of the committed state (not updated for this transaction). More...
 
int64_t getDataSizeChange () const
 Cumulative change (delta) of data size by this pending transaction (uncommitted). More...
 

Detailed Description

Provides RAII wrapper for an active database transaction on the current thread (do not use across threads). A Transaction object is considered a "top level transaction" if it is the first one on the call stack in the thread. If the thread already has an ongoing Transaction, additional Transaction instances are considered "inner transactions".

The top level transaction defines the actual transaction scope on the DB level. Internally, the top level Transaction object manages (creates and destroys) a Transaction object. Inner transactions use the Transaction object of the top level Transaction.

For write transactions, the top level call to success() actually commits the underlying Transaction. If inner transactions are spawned, all of them must call success() in order for the top level transaction to be successful and actually commit.

Constructor & Destructor Documentation

◆ Transaction() [1/3]

obx::Transaction::Transaction ( Store store,
TxMode  mode 
)

◆ Transaction() [2/3]

obx::Transaction::Transaction ( const Transaction )
delete

Delete because the default copy constructor can break things (i.e. a Transaction can not be copied).

◆ Transaction() [3/3]

obx::Transaction::Transaction ( Transaction &&  source)
inlinenoexcept

Move constructor, used by Store::tx()

◆ ~Transaction()

virtual obx::Transaction::~Transaction ( )
inlinevirtual

Never throws.

Member Function Documentation

◆ close()

void obx::Transaction::close ( )

Explicit close to free up resources; unlike closeNoThrow() (which is also called by the destructor), this version throw in the unlikely case of failing. It's OK to call this method multiple times; additional calls will have no effect.

◆ closeNoThrow()

obx_err obx::Transaction::closeNoThrow ( )

Explicit close to free up resources (non-throwing version). It's OK to call this method multiple times; additional calls will have no effect.

◆ cPtr()

OBX_txn* obx::Transaction::cPtr ( ) const

The transaction pointer of the ObjectBox C API.

Exceptions
ifthis Transaction was already closed or moved

◆ getDataSizeChange()

int64_t obx::Transaction::getDataSizeChange ( ) const

Cumulative change (delta) of data size by this pending transaction (uncommitted).

◆ getDataSizeCommitted()

uint64_t obx::Transaction::getDataSizeCommitted ( ) const

The data size of the committed state (not updated for this transaction).

◆ isActive()

bool obx::Transaction::isActive ( )
inline

A Transaction is active if it was not ended via success(), close() or moving.

◆ operator=()

Transaction& obx::Transaction::operator= ( Transaction  source)

Copy-and-swap style.

◆ success()

void obx::Transaction::success ( )

"Finishes" this write transaction successfully; performs a commit if this is the top level transaction and all inner transactions (if any) were also successful. This object will also be "closed".

Exceptions
Exceptionif this is not a write TX or it was closed before (e.g. via success()).