MDBX Containers
Loading...
Searching...
No Matches
mdbxc::Connection Class Reference

Manages a single MDBX environment and an optional read-only transaction. More...

#include <Connection.hpp>

Inheritance diagram for mdbxc::Connection:
mdbxc::TransactionTracker

Public Member Functions

 Connection ()=default
 Default constructor.
 
 Connection (const Config &config)
 Constructs a connection using the given MDBX configuration.
 
 ~Connection ()
 Destructor. Closes the MDBX environment and aborts any open transactions.
 
void configure (const Config &config)
 Sets the MDBX configuration (must be called before connect()).
 
void connect ()
 Connects to the database using the current configuration.
 
void connect (const Config &config)
 Sets configuration and connects in one step.
 
void disconnect ()
 Disconnects from the MDBX environment and releases resources.
 
bool is_connected () const
 Checks whether the environment is currently open.
 
Transaction transaction (TransactionMode mode=TransactionMode::WRITABLE)
 Creates a RAII transaction object.
 
void begin (TransactionMode mode=TransactionMode::WRITABLE)
 Begins a manual transaction (must be committed or rolled back later).
 
void commit ()
 Commits the current manual transaction.
 
void rollback ()
 Rolls back the current manual transaction.
 
std::shared_ptr< Transactioncurrent_txn () const
 Returns the transaction associated with the current thread.
 
MDBX_env * env_handle () noexcept
 Returns the environment handle.
 

Static Public Member Functions

static std::shared_ptr< Connectioncreate (const Config &config)
 Creates and connects a new shared Connection instance.
 

Private Types

using config_t = std::unique_ptr<Config>
 

Private Member Functions

void initialize ()
 Initializes the environment and sets up read-only transaction.
 
void cleanup (bool use_throw=true)
 Safely closes the environment and aborts transaction if needed.
 
void db_init ()
 Initializes MDBX environment and opens a read-only transaction.
 
- Private Member Functions inherited from mdbxc::TransactionTracker
void bind_txn (MDBX_txn *txn)
 Registers a transaction for a specific thread.
 
void unbind_txn ()
 Unregisters a transaction for a specific thread.
 
MDBX_txn * thread_txn () const
 Retrieves the transaction associated with the current thread.
 
virtual ~TransactionTracker ()=default
 

Private Attributes

MDBX_env * m_env = nullptr
 Pointer to the MDBX environment handle.
 
std::mutex m_mdbx_mutex
 Mutex for thread-safe access.
 
std::unordered_map< std::thread::id, std::shared_ptr< Transaction > > m_transactions
 
config_t m_config
 Database configuration object.
 

Friends

class BaseTable
 
class Transaction
 

Detailed Description

Manages a single MDBX environment and an optional read-only transaction.

Definition at line 13 of file Connection.hpp.

Member Typedef Documentation

◆ config_t

using mdbxc::Connection::config_t = std::unique_ptr<Config>
private

Definition at line 89 of file Connection.hpp.

Constructor & Destructor Documentation

◆ Connection() [1/2]

mdbxc::Connection::Connection ( )
default

Default constructor.

◆ Connection() [2/2]

mdbxc::Connection::Connection ( const Config & config)
inlineexplicit

Constructs a connection using the given MDBX configuration.

Parameters
configConfiguration used to initialize the environment.

Definition at line 17 of file Connection.ipp.

◆ ~Connection()

mdbxc::Connection::~Connection ( )
inline

Destructor. Closes the MDBX environment and aborts any open transactions.

Definition at line 21 of file Connection.ipp.

Member Function Documentation

◆ begin()

void mdbxc::Connection::begin ( TransactionMode mode = TransactionMode::WRITABLE)
inline

Begins a manual transaction (must be committed or rolled back later).

Parameters
modeThe transaction mode (default: WRITABLE).
Exceptions
MdbxExceptionif the transaction is already started or if beginning fails.
std::logic_error

Definition at line 73 of file Connection.ipp.

◆ cleanup()

void mdbxc::Connection::cleanup ( bool use_throw = true)
inlineprivate

Safely closes the environment and aborts transaction if needed.

Parameters
use_throwIf true, throw on error; otherwise suppress exceptions.

Definition at line 128 of file Connection.ipp.

◆ commit()

void mdbxc::Connection::commit ( )
inline

Commits the current manual transaction.

Exceptions
MdbxExceptionif no transaction is active or commit/reset fails.

Definition at line 84 of file Connection.ipp.

◆ configure()

void mdbxc::Connection::configure ( const Config & config)
inline

Sets the MDBX configuration (must be called before connect()).

Parameters
configNew configuration to apply.

Definition at line 32 of file Connection.ipp.

◆ connect() [1/2]

void mdbxc::Connection::connect ( )
inline

Connects to the database using the current configuration.

Exceptions
MdbxExceptionon configuration or environment errors.

Definition at line 41 of file Connection.ipp.

◆ connect() [2/2]

void mdbxc::Connection::connect ( const Config & config)
inline

Sets configuration and connects in one step.

Parameters
configConfiguration to use.

Definition at line 48 of file Connection.ipp.

◆ create()

std::shared_ptr< Connection > mdbxc::Connection::create ( const Config & config)
inlinestatic

Creates and connects a new shared Connection instance.

Parameters
configConfiguration to use for initialization.
Returns
Shared pointer to the created Connection.

Definition at line 26 of file Connection.ipp.

◆ current_txn()

std::shared_ptr< Transaction > mdbxc::Connection::current_txn ( ) const
inline

Returns the transaction associated with the current thread.

Returns
Shared pointer to the active Transaction or nullptr.

Definition at line 106 of file Connection.ipp.

◆ db_init()

void mdbxc::Connection::db_init ( )
inlineprivate

Initializes MDBX environment and opens a read-only transaction.

Definition at line 138 of file Connection.ipp.

◆ disconnect()

void mdbxc::Connection::disconnect ( )
inline

Disconnects from the MDBX environment and releases resources.

Exceptions
MdbxExceptionif closing the environment fails.

Definition at line 59 of file Connection.ipp.

◆ env_handle()

MDBX_env * mdbxc::Connection::env_handle ( )
inlinenoexcept

Returns the environment handle.

Returns
MDBX environment pointer.

Definition at line 112 of file Connection.ipp.

◆ initialize()

void mdbxc::Connection::initialize ( )
inlineprivate

Initializes the environment and sets up read-only transaction.

Definition at line 116 of file Connection.ipp.

◆ is_connected()

bool mdbxc::Connection::is_connected ( ) const
inline

Checks whether the environment is currently open.

Returns
true if connected, false otherwise.

Definition at line 64 of file Connection.ipp.

◆ rollback()

void mdbxc::Connection::rollback ( )
inline

Rolls back the current manual transaction.

Exceptions
MdbxExceptionif no transaction is active or rollback/reset fails.

Definition at line 95 of file Connection.ipp.

◆ transaction()

Transaction mdbxc::Connection::transaction ( TransactionMode mode = TransactionMode::WRITABLE)
inline

Creates a RAII transaction object.

Parameters
modeTransaction mode to open (default: WRITABLE).
Exceptions
MdbxExceptionon MDBX errors.
Returns
Transaction guard managing the MDBX_txn handle.

Definition at line 69 of file Connection.ipp.

Friends And Related Symbol Documentation

◆ BaseTable

friend class BaseTable
friend

Definition at line 14 of file Connection.hpp.

◆ Transaction

friend class Transaction
friend

Definition at line 81 of file Connection.hpp.

Member Data Documentation

◆ m_config

config_t mdbxc::Connection::m_config
private

Database configuration object.

Definition at line 91 of file Connection.hpp.

◆ m_env

MDBX_env* mdbxc::Connection::m_env = nullptr
private

Pointer to the MDBX environment handle.

Definition at line 83 of file Connection.hpp.

◆ m_mdbx_mutex

std::mutex mdbxc::Connection::m_mdbx_mutex
mutableprivate

Mutex for thread-safe access.

Definition at line 84 of file Connection.hpp.

◆ m_transactions

std::unordered_map<std::thread::id, std::shared_ptr<Transaction> > mdbxc::Connection::m_transactions
private

Definition at line 85 of file Connection.hpp.


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