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.
The URL given to a Connection Pool at creation time specifies a database connection in the standard URL format:
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.
Example URL for MySQL:
Or using the auth-part of the URL:
See mysql options for all properties that can be set for a mysql connection URL.
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:
serialized=true
- Make SQLite switch to serialized mode if value is true, otherwise multi-thread mode is used (the default).shared-cache=true
- Make SQLite use shared-cache if value is true. Using shared cache can significantly reduce database lock errors in some scenarios where two or more connections might write to the database. It is also recommended to build libsqlite and libzdb with unlock notify.Example URL for SQLite (with recommended pragmas):
Example URL for PostgreSQL:
Or using the auth-part and SSL:
See postgresql options for all properties that can be set for a postgresql connection URL.
Example URL for Oracle:
Or using the auth-part and SYSDBA role:
See oracle options for all properties that can be set for an oracle connection URL.
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:
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.
Represents a database connection pool. More...
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)> |
CONNECTIONPOOL_TYPE | getType () const noexcept |
Retrieves the database type for this ConnectionPool. | |
const URL & | getURL () 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. | |
using AbortHandler = std::function<void(std::string_view)> |
|
explicit |
Constructs a ConnectionPool with the given URL string.
url | The database connection URL string. |
sql_exception | If the URL is invalid. |
|
explicit |
Constructs a ConnectionPool with a URL object.
url | The database connection URL object to move from. |
sql_exception | If the URL is invalid. |
|
noexcept |
Retrieves the database type for this ConnectionPool.
|
nodiscardnoexcept |
|
noexcept |
Sets the number of initial connections in the pool.
initialConnections | The number of initial connections. |
|
nodiscardnoexcept |
Gets the number of initial connections in the pool.
|
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.
maxConnections | The maximum number of connections. |
|
nodiscardnoexcept |
Gets the maximum number of connections in the pool.
|
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.
connectionTimeout | The timeout value in seconds. It is a checked runtime error for connectionTimeout to be <= 0 |
|
nodiscardnoexcept |
Gets the connection timeout value.
|
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.
abortHandler | The handler function to call on fatal errors. |
|
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.
sweepInterval | Number of seconds between sweeps of the reaper thread. Set to 0 or a negative value to disable the reaper thread, before calling ConnectionPool::start(). |
|
nodiscardnoexcept |
Gets the current number of connections in the pool.
|
nodiscardnoexcept |
Gets the number of active connections in the pool.
|
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.
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().
sql_exception | If a database error occurs. |
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.
sql_exception | If there are active connections. |
|
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:
sql_exception | If a database connection cannot be obtained |
|
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.
con | The Connection to return. |
|
noexcept |
Reaps inactive connections in the pool.
Copyright © Tildeslash Ltd. All rights reserved.