Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
transaction_guard Class Reference

RAII transaction guard that rolls back on destruction unless committed. More...

Collaboration diagram for transaction_guard:
Collaboration graph

Public Member Functions

 transaction_guard (std::shared_ptr< database_manager > mgr)
 
 ~transaction_guard ()
 
void commit ()
 
 transaction_guard (const transaction_guard &)=delete
 
transaction_guardoperator= (const transaction_guard &)=delete
 

Private Attributes

std::shared_ptr< database_managermgr_
 
bool committed_
 

Detailed Description

RAII transaction guard that rolls back on destruction unless committed.

This pattern ensures that transactions are never left dangling even when an exception or early return occurs.

Definition at line 36 of file transaction_management.cpp.

Constructor & Destructor Documentation

◆ transaction_guard() [1/2]

transaction_guard::transaction_guard ( std::shared_ptr< database_manager > mgr)
inlineexplicit

Definition at line 39 of file transaction_management.cpp.

40 : mgr_(std::move(mgr)), committed_(false)
41 {
42 auto result = mgr_->begin_transaction();
43 if (!result.is_ok())
44 {
45 throw std::runtime_error("Failed to begin transaction");
46 }
47 }
std::shared_ptr< database_manager > mgr_

References mgr_.

◆ ~transaction_guard()

transaction_guard::~transaction_guard ( )
inline

Definition at line 49 of file transaction_management.cpp.

50 {
51 if (!committed_ && mgr_->in_transaction())
52 {
53 std::cout << " [guard] Rolling back uncommitted transaction" << std::endl;
54 mgr_->rollback_transaction();
55 }
56 }

References committed_, and mgr_.

◆ transaction_guard() [2/2]

transaction_guard::transaction_guard ( const transaction_guard & )
delete

Member Function Documentation

◆ commit()

void transaction_guard::commit ( )
inline

Definition at line 58 of file transaction_management.cpp.

59 {
60 auto result = mgr_->commit_transaction();
61 if (result.is_ok())
62 {
63 committed_ = true;
64 }
65 else
66 {
67 throw std::runtime_error("Failed to commit transaction");
68 }
69 }

References committed_, and mgr_.

Referenced by main().

Here is the caller graph for this function:

◆ operator=()

transaction_guard & transaction_guard::operator= ( const transaction_guard & )
delete

Member Data Documentation

◆ committed_

bool transaction_guard::committed_
private

Definition at line 77 of file transaction_management.cpp.

Referenced by commit(), and ~transaction_guard().

◆ mgr_

std::shared_ptr<database_manager> transaction_guard::mgr_
private

Definition at line 76 of file transaction_management.cpp.

Referenced by commit(), transaction_guard(), and ~transaction_guard().


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