ConnectionPool

Detailed Description

Represents a database connection pool.

A ConnectionPool can be used to obtain connections to a database and execute statements. This class opens a number of database connections and allows callers to obtain and use a database connection in a reentrant manner. Applications can instantiate as many ConnectionPool objects as needed and against as many different database systems as needed.

Connection URL:

The URL given to a Connection Pool at creation time specifies a database connection in the standard URL format:

database://[user:password@][host][:port]/database[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

The property names user and password are always recognized and specify how to log in to the database. Other properties depend on the database server in question. Username and password can alternatively be specified in the auth-part of the URL. If port number is omitted, the default port number for the database server is used.

MySQL:

Example URL for MySQL:

mysql://localhost:3306/test?user=root&password=swordfish

Or using the auth-part of the URL:

mysql://root:swordfish@localhost:3306/test

See mysql options for all properties that can be set for a mysql connection URL.

SQLite:

For SQLite, the URL should specify a database file. SQLite uses pragma commands for performance tuning and other special purpose database commands. Pragma syntax in the form name=value can be added as properties to the URL. In addition to pragmas, the following properties are supported:

  • heap_limit=value - Make SQLite auto-release unused memory if memory usage goes above the specified value [KB].
  • serialized=true - Make SQLite switch to serialized mode if value is true, otherwise multi-thread mode is used (the default).

Example URL for SQLite (with recommended pragmas):

sqlite:///var/sqlite/test.db?synchronous=normal&foreign_keys=on&journal_mode=wal&temp_store=memory

PostgreSQL:

Example URL for PostgreSQL:

postgresql://localhost:5432/test?user=root&password=swordfish

Or using the auth-part and SSL:

postgresql://root:swordfish@localhost/test?use-ssl=true

See postgresql options for all properties that can be set for a postgresql connection URL.

Oracle:

Example URL for Oracle:

oracle://localhost:1521/servicename?user=scott&password=tiger

Or using the auth-part and SYSDBA role:

oracle://sys:password@localhost:1521/servicename?sysdba=true

See oracle options for all properties that can be set for an oracle connection URL.

Pool Management:

The pool is designed to dynamically manage the number of active connections based on usage patterns. A reaper thread is automatically started when the pool is initialized, performing two functions:

  1. Sweep through the pool at regular intervals (default every 60 seconds) to close connections that have been inactive for a specified time (default 90 seconds).
  2. Perform periodic validation (ping test) on idle connections to ensure they remain valid and responsive.

Realtime inspection:

Three methods can be used to inspect the pool at runtime. The method size() returns the number of connections in the pool, that is, both active and inactive connections. The method active() returns the number of active connections, i.e., those connections in current use by your application. The method isFull() can be used to check if the pool is full and unable to return a connection.

Example Usage:

ConnectionPool pool("mysql://localhost/test?user=root&password=swordfish");
pool.start();
// ...
Connection con = pool.getConnection();
ResultSet result = con.executeQuery("SELECT id, name, photo FROM employee WHERE salary > ?", 50000);
while (result.next()) {
int id = result.getInt("id");
auto name = result.getString("name");
auto photo = result.getBlob("photo");
// Process data...
}
Represents a database connection pool.
Definition zdbpp.h:1771
Represents a connection to a SQL database system.
Definition zdbpp.h:1333
ResultSet executeQuery(const std::string &sql, Args &&... args)
Executes a SQL query and returns a ResultSet.
Definition zdbpp.h:1563
Represents a database result set.
Definition zdbpp.h:590
int getInt(int columnIndex)
Gets the designated column's value as an int.
Definition zdbpp.h:731
bool next()
Moves the cursor to the next row.
Definition zdbpp.h:677
std::optional< std::string_view > getString(int columnIndex)
Gets the designated column's value as a string.
Definition zdbpp.h:707
std::optional< std::span< const std::byte > > getBlob(int columnIndex)
Gets the designated column's value as a byte span.
Definition zdbpp.h:795
Note
This ConnectionPool is thread-safe.
Warning
A ConnectionPool is neither copyable nor movable. It is designed to be a long-lived object that manages database connections throughout the lifetime of your application. Typically, you would instantiate one or more ConnectionPool objects as part of a resource management class or in the global scope of your application.

Public Member Functions

 ConnectionPool (const std::string &url)
 Constructs a ConnectionPool with the given URL string.
 
 ConnectionPool (URL &&url)
 Constructs a ConnectionPool with a URL object.
 
int size () noexcept
 Gets the current number of connections in the pool.
 
int active () noexcept
 Gets the number of active connections in the pool.
 
bool isFull () noexcept
 Checks if the pool is full.
 
void start ()
 Prepares the pool for active use.
 
void stop ()
 Gracefully terminates the pool.
 
Connection getConnection ()
 Gets a connection from the pool.
 
void returnConnection (Connection &con) noexcept
 Returns a connection to the pool.
 
int reapConnections () noexcept
 Reaps inactive connections in the pool.
 

Properties

using AbortHandler = std::function<void(std::string_view)>
 
const URLgetURL () const noexcept
 Gets the URL of the connection pool.
 
void setInitialConnections (int initialConnections) noexcept
 Sets the number of initial connections in the pool.
 
int getInitialConnections () noexcept
 Gets the number of initial connections in the pool.
 
void setMaxConnections (int maxConnections) noexcept
 Sets the maximum number of connections in the pool.
 
int getMaxConnections () noexcept
 Gets the maximum number of connections in the pool.
 
void setConnectionTimeout (int connectionTimeout) noexcept
 Sets the connection inactive timeout value in seconds.
 
int getConnectionTimeout () noexcept
 Gets the connection timeout value.
 
void setAbortHandler (AbortHandler abortHandler=nullptr) noexcept
 Sets the function to call if a fatal error occurs in the library.
 
void setReaper (int sweepInterval) noexcept
 Customizes the reaper thread behavior or disables it.
 

Member Typedef Documentation

◆ AbortHandler

using AbortHandler = std::function<void(std::string_view)>

Constructor & Destructor Documentation

◆ ConnectionPool() [1/2]

ConnectionPool ( const std::string & url)
explicit

Constructs a ConnectionPool with the given URL string.

Parameters
urlThe database connection URL string.
Exceptions
sql_exceptionIf the URL is invalid.

◆ ConnectionPool() [2/2]

ConnectionPool ( URL && url)
explicit

Constructs a ConnectionPool with a URL object.

Parameters
urlThe database connection URL object to move from.
Exceptions
sql_exceptionIf the URL is invalid.

Member Function Documentation

◆ getURL()

const URL & getURL ( ) const
nodiscardnoexcept

Gets the URL of the connection pool.

Returns
The URL of the connection pool.

◆ setInitialConnections()

void setInitialConnections ( int initialConnections)
noexcept

Sets the number of initial connections in the pool.

Parameters
initialConnectionsThe number of initial connections.

◆ getInitialConnections()

int getInitialConnections ( )
nodiscardnoexcept

Gets the number of initial connections in the pool.

Returns
The number of initial connections.

◆ setMaxConnections()

void setMaxConnections ( int maxConnections)
noexcept

Sets the maximum number of connections in the pool.

If max connections has been reached, getConnection() will fail on the next call. It is a checked runtime error for maxConnections to be less than initialConnections.

Parameters
maxConnectionsThe maximum number of connections.

◆ getMaxConnections()

int getMaxConnections ( )
nodiscardnoexcept

Gets the maximum number of connections in the pool.

Returns
The maximum number of connections.

◆ setConnectionTimeout()

void setConnectionTimeout ( int connectionTimeout)
noexcept

Sets the connection inactive timeout value in seconds.

The method reapConnections(), if called, will close inactive connections in the pool which have not been in use for connectionTimeout seconds. The default connectionTimeout is 90 seconds.

Parameters
connectionTimeoutThe timeout value in seconds. It is a checked runtime error for connectionTimeout to be <= 0

◆ getConnectionTimeout()

int getConnectionTimeout ( )
nodiscardnoexcept

Gets the connection timeout value.

Returns
The connection timeout value in seconds.

◆ setAbortHandler()

void setAbortHandler ( AbortHandler abortHandler = nullptr)
noexcept

Sets the function to call if a fatal error occurs in the library.

In practice this means Out-Of-Memory errors or uncaught exceptions. Clients may optionally provide this function. If not provided the library will call abort(3) or exit(1) upon encountering a fatal error. It is an unchecked runtime error to continue using the library after the abortHandler was called.

Parameters
abortHandlerThe handler function to call on fatal errors.

◆ setReaper()

void setReaper ( int sweepInterval)
noexcept

Customizes the reaper thread behavior or disables it.

By default, a reaper thread is automatically started when the pool is initialized, with a default sweep interval of 60 seconds. This method allows you to change the sweep interval or disable the reaper entirely.

The reaper thread closes inactive Connections in the pool, down to the initial connection count. An inactive Connection is closed if its connectionTimeout has expired or if it fails a ping test. Active Connections (those in current use) are never closed by this thread.

This method can be called before or after ConnectionPool::start(). If called after start, the changes will take effect on the next sweep cycle.

Parameters
sweepIntervalNumber of seconds between sweeps of the reaper thread. Set to 0 or a negative value to disable the reaper thread, before calling ConnectionPool::start().

◆ size()

int size ( )
nodiscardnoexcept

Gets the current number of connections in the pool.

Returns
The total number of connections in the pool.

◆ active()

int active ( )
nodiscardnoexcept

Gets the number of active connections in the pool.

Returns
The number of active connections in the pool.

◆ isFull()

bool isFull ( )
nodiscardnoexcept

Checks if the pool is full.

The pool is full if the number of active connections equals max connections and the pool is unable to return a connection.

Returns
true if pool is full, false otherwise
Note
A full pool is unlikely to occur in practice if you ensure that connections are returned to the pool after use.

◆ start()

void start ( )

Prepares the pool for active use.

This method must be called before the pool is used. It will connect to the database server, create the initial connections for the pool, and start the reaper thread with default settings, unless previously disabled via setReaper().

Exceptions
sql_exceptionIf a database error occurs.

◆ stop()

void stop ( )

Gracefully terminates the pool.

This method should be the last one called on a given instance of this component. Calling this method closes down all connections in the pool, disconnects the pool from the database server, and stops the reaper thread if it was started.

Exceptions
sql_exceptionIf there are active connections.

◆ getConnection()

Connection getConnection ( )
nodiscard

Gets a connection from the pool.

The returned Connection is guaranteed to be alive and connected to the database.

An sql_exception may be thrown if the pool is full or if a database error occurs (e.g., network issues, database unavailability).

Here's a basic example of how to use getConnection and handle potential errors:

try {
Connection con = pool.getConnection();
// Use the connection ...
} catch (const sql_exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
Exception class for SQL related errors.
Definition zdbpp.h:275
Returns
A Connection object.
Exceptions
sql_exceptionIf a database connection cannot be obtained

◆ returnConnection()

void returnConnection ( Connection & con)
noexcept

Returns a connection to the pool.

The same as calling Connection::close() on a connection. If the connection is in an uncommitted transaction, rollback is called. It is an unchecked error to attempt to use the Connection after this method is called.

Parameters
conThe Connection to return.

◆ reapConnections()

int reapConnections ( )
noexcept

Reaps inactive connections in the pool.

Returns
The number of connections reaped.

Copyright © Tildeslash Ltd. All rights reserved.