KDbTransactionGuard

Search for usage in LXR

KDbTransactionGuard Class Reference

#include <KDbTransactionGuard.h>

Public Member Functions

 KDbTransactionGuard ()
 
 KDbTransactionGuard (const KDbTransaction &transaction)
 
 KDbTransactionGuard (KDbConnection *connection)
 
 ~KDbTransactionGuard ()
 
bool commit (KDbTransaction::CommitOptions options=KDbTransaction::CommitOptions())
 
void doNothing ()
 
bool rollback (KDbTransaction::CommitOptions options=KDbTransaction::CommitOptions())
 
void setTransaction (const KDbTransaction &transaction)
 
const KDbTransaction transaction () const
 

Detailed Description

KDbTransactionGuard class is a convenience class that simplifies handling transactions.

KDbTransactionGuard can be used in two ways:

  • start a new transaction in constructor and call commit or allow to rollback on destruction,
  • use already started transaction and call commit or allow to rollback on destruction.

In either case if the guarded transaction is committed or rolled back outside the KDbTransactionGuard object in the meantime, nothing happens on KDbTransactionGuard's destruction.

Example usage: void MyClass::myMethod() { KDbTransaction transaction = connection->beginTransaction(); KDbTransactionGuard tg(transaction); // <– Some code that operates within the transaction here if (failure_condition) return; // After this return tg will be destroyed; connection->rollbackTransaction() // will be called automatically // success_condition: tg.commit(); // Now tg won't do anything because transaction no longer exists }

Definition at line 51 of file KDbTransactionGuard.h.

Constructor & Destructor Documentation

◆ KDbTransactionGuard() [1/3]

KDbTransactionGuard::KDbTransactionGuard ( KDbConnection * connection)
explicit

Starts a new transaction for given connection and guards it.

When the new guard object is created a new transaction is started for connection connection using KDbConnection::beginTransaction(). Started KDbTransaction handle is available via transaction(). Unassigned guard is created if connection is nullptr or if beginTransaction() fails.

Definition at line 203 of file KDbTransaction.cpp.

◆ KDbTransactionGuard() [2/3]

KDbTransactionGuard::KDbTransactionGuard ( const KDbTransaction & transaction)
explicit

Creates a new guard for already started transaction.

If transaction is not started i.e. transaction is null or not active, it will not be guarded.

setTransaction() can be used later to assign active transaction.

Definition at line 211 of file KDbTransaction.cpp.

◆ KDbTransactionGuard() [3/3]

KDbTransactionGuard::KDbTransactionGuard ( )

Creates a new guard without assigning transaction.

setTransaction() can be used later to assign active transaction.

Definition at line 217 of file KDbTransaction.cpp.

◆ ~KDbTransactionGuard()

KDbTransactionGuard::~KDbTransactionGuard ( )

Roll backs not committed transaction unless doNothing() was called before.

If doNothing() was called, transaction is left unaffected.

Definition at line 222 of file KDbTransaction.cpp.

Member Function Documentation

◆ commit()

bool KDbTransactionGuard::commit ( KDbTransaction::CommitOptions options = KDbTransaction::CommitOptions())

Commits the guarded transaction.

This is an equivalent of transaction().connection()->commitTransaction(transaction(), options) provided for convenience.

false is also returned if transaction().connection() is nullptr.

Definition at line 240 of file KDbTransaction.cpp.

◆ doNothing()

void KDbTransactionGuard::doNothing ( )

Deativates the transaction guard.

This means nothing will happen on guard's destruction.

Definition at line 256 of file KDbTransaction.cpp.

◆ rollback()

bool KDbTransactionGuard::rollback ( KDbTransaction::CommitOptions options = KDbTransaction::CommitOptions())

Rolls back the guarded transaction.

This is an equivalent of transaction().connection()->rollbackTransaction(transaction(), options) provided for convenience.

false is also returned if transaction().connection() is nullptr.

Since
3.1

Definition at line 248 of file KDbTransaction.cpp.

◆ setTransaction()

void KDbTransactionGuard::setTransaction ( const KDbTransaction & transaction)

Assigns transaction to this guard.

Previously assigned transaction will be unassigned from this guard without commiting or rolling back.

Definition at line 235 of file KDbTransaction.cpp.

◆ transaction()

const KDbTransaction KDbTransactionGuard::transaction ( ) const

Returns transaction that is controlled by this guard.

Null object is returned if no transaction is guarded.

Definition at line 261 of file KDbTransaction.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.