![]() |
Kea 2.7.7
|
Common database connection class. More...
#include <database_connection.h>
Classes | |
class | EnterTest |
RAII device to set the test mode. More... | |
Public Types | |
typedef std::map< std::string, std::string > | ParameterMap |
Database configuration parameter map. | |
Public Member Functions | |
DatabaseConnection (const ParameterMap ¶meters, DbCallback callback=DbCallback()) | |
Constructor. | |
virtual | ~DatabaseConnection () |
Destructor. | |
void | checkUnusable () |
Throws an exception if the connection is not usable. | |
bool | configuredReadOnly () const |
Convenience method checking if database should be opened with read only access. | |
std::string | getParameter (const std::string &name) const |
Returns value of a connection parameter. | |
bool | isUnusable () |
Flag which indicates if connection is unusable. | |
virtual void | makeReconnectCtl (const std::string &timer_name, unsigned int id) |
Instantiates a ReconnectCtl based on the connection's reconnect parameters. | |
util::ReconnectCtlPtr | reconnectCtl () |
The reconnect settings. | |
Static Public Member Functions | |
static isc::asiolink::IOServicePtr & | getIOService () |
Returns pointer to the IO service. | |
static bool | invokeDbFailedCallback (const util::ReconnectCtlPtr &db_reconnect_ctl) |
Invokes the connection's restore failed connectivity callback. | |
static bool | invokeDbLostCallback (const util::ReconnectCtlPtr &db_reconnect_ctl) |
Invokes the connection's lost connectivity callback. | |
static bool | invokeDbRecoveredCallback (const util::ReconnectCtlPtr &db_reconnect_ctl) |
Invokes the connection's restored connectivity callback. | |
static ParameterMap | parse (const std::string &dbaccess) |
Parse database access string. | |
static std::string | redactedAccessString (const ParameterMap ¶meters) |
Redact database access string. | |
static void | setIOService (const isc::asiolink::IOServicePtr &io_service) |
Sets IO service to be used by the database backends. | |
static isc::data::ElementPtr | toElement (const ParameterMap ¶ms) |
Unparse a parameter map. | |
static isc::data::ElementPtr | toElementDbAccessString (const std::string &dbaccess) |
Unparse an access string. | |
Static Public Attributes | |
static DbCallback | db_failed_callback_ = 0 |
Optional callback function to invoke if an opened connection recovery failed. | |
static DbCallback | db_lost_callback_ = 0 |
Optional callback function to invoke if an opened connection is lost. | |
static DbCallback | db_recovered_callback_ = 0 |
Optional callback function to invoke if an opened connection recovery succeeded. | |
static const time_t | MAX_DB_TIME = 2147483647 |
Defines maximum value for time that can be reliably stored. | |
static bool | retry_ = false |
Flag which indicates if the database connection should be retried on fail. | |
static bool | test_mode_ = false |
Test mode flag (default false). | |
Protected Member Functions | |
void | markUnusable () |
Sets the unusable flag to true. | |
Protected Attributes | |
DbCallback | callback_ |
The callback used to recover the connection. | |
Common database connection class.
This class provides functions that are common for establishing connection with different types of databases; enables operations on access parameters strings. In particular, it provides a way to parse parameters in key=value format. This class is expected to be a base class for all isc::dhcp::LeaseMgr and possibly isc::dhcp::BaseHostDataSource derived classes.
Definition at line 125 of file database_connection.h.
std::map<std::string, std::string> isc::db::DatabaseConnection::ParameterMap |
Database configuration parameter map.
Definition at line 137 of file database_connection.h.
|
inline |
Constructor.
parameters | A data structure relating keywords and values concerned with the database. |
callback | The connection recovery callback. |
Definition at line 144 of file database_connection.h.
|
inlinevirtual |
Destructor.
Definition at line 150 of file database_connection.h.
|
inline |
Throws an exception if the connection is not usable.
DbConnectionUnusable |
Definition at line 266 of file database_connection.h.
References isc_throw.
Referenced by isc::db::MySqlConnection::commit(), isc::db::PgSqlConnection::commit(), isc::db::PgSqlConnection::executePreparedStatement(), isc::db::PgSqlConnection::executeSQL(), isc::db::MySqlConnection::insertQuery(), isc::db::MySqlConnection::rollback(), isc::db::PgSqlConnection::rollback(), isc::db::MySqlConnection::selectQuery(), isc::db::MySqlConnection::startTransaction(), isc::db::PgSqlConnection::startTransaction(), and isc::db::MySqlConnection::updateDeleteQuery().
bool isc::db::DatabaseConnection::configuredReadOnly | ( | ) | const |
Convenience method checking if database should be opened with read only access.
Definition at line 136 of file database_connection.cc.
References getParameter(), and isc_throw.
|
inlinestatic |
Returns pointer to the IO service.
Definition at line 240 of file database_connection.h.
Referenced by isc::db::MySqlConnection::ensureSchemaVersion(), and isc::db::PgSqlConnection::ensureSchemaVersion().
std::string isc::db::DatabaseConnection::getParameter | ( | const std::string & | name | ) | const |
Returns value of a connection parameter.
name | Name of the parameter which value should be returned. |
BadValue | if parameter is not found |
Definition at line 32 of file database_connection.cc.
References isc_throw.
Referenced by isc::dhcp::Memfile_LeaseMgr::Memfile_LeaseMgr(), configuredReadOnly(), isc::dhcp::Memfile_LeaseMgr::getVersion(), makeReconnectCtl(), and isc::db::MySqlConnection::openDatabase().
|
static |
Invokes the connection's restore failed connectivity callback.
Definition at line 213 of file database_connection.cc.
References db_failed_callback_.
|
static |
Invokes the connection's lost connectivity callback.
Definition at line 195 of file database_connection.cc.
References db_lost_callback_.
|
static |
Invokes the connection's restored connectivity callback.
Definition at line 204 of file database_connection.cc.
References db_recovered_callback_.
|
inline |
Flag which indicates if connection is unusable.
Definition at line 275 of file database_connection.h.
|
virtual |
Instantiates a ReconnectCtl based on the connection's reconnect parameters.
timer_name | of the timer used for the ReconnectCtl object. |
id | the ID of the manager. |
Definition at line 155 of file database_connection.cc.
References getParameter(), and isc::util::ReconnectCtl::onFailActionFromText().
Referenced by isc::db::MySqlConnection::getVersion(), and isc::db::PgSqlConnection::getVersion().
|
inlineprotected |
Sets the unusable flag to true.
Definition at line 302 of file database_connection.h.
Referenced by isc::db::MySqlConnection::checkError(), and isc::db::PgSqlConnection::checkStatementError().
|
static |
Parse database access string.
Parses the string of "keyword=value" pairs and separates them out into the map. A value of the password parameter may include whitespace in which case it must be surrounded by apostrophes.
dbaccess | Database access string. |
Definition at line 41 of file database_connection.cc.
References isc::db::DB_LOG< log_type >::arg(), isc::data::DefaultCredentials::check(), isc::db::DB_INVALID_ACCESS, isc_throw, and isc::util::str::trim().
Referenced by isc::dhcp::HostDataSourceFactory::add(), isc::cb::BaseConfigBackendMgr< ConfigBackendPoolType >::addBackend(), isc::dhcp::LeaseMgrFactory::create(), isc::dhcp::CfgDbAccess::createManagers(), isc::cb::BaseConfigBackendPool< ConfigBackendType >::del(), isc::dhcp::HostDataSourceFactory::del(), isc::process::ConfigDbInfo::setAccessString(), and toElementDbAccessString().
|
inline |
The reconnect settings.
Definition at line 162 of file database_connection.h.
Referenced by isc::db::MySqlConnection::openDatabase(), isc::db::MySqlConnection::startRecoverDbConnection(), and isc::db::PgSqlConnection::startRecoverDbConnection().
|
static |
Redact database access string.
Takes the database parameters and returns a database access string passwords replaced by asterisks. This string is used in log messages.
parameters | Database access parameters (output of "parse"). |
Definition at line 108 of file database_connection.cc.
Referenced by isc::dhcp::LeaseMgrFactory::create(), isc::dhcp::CfgDbAccess::createManagers(), isc::dhcp::Memfile_LeaseMgr::factory(), and isc::process::ConfigDbInfo::redactedAccessString().
|
inlinestatic |
Sets IO service to be used by the database backends.
io_service | IOService object, used for all ASIO operations. |
Definition at line 235 of file database_connection.h.
Referenced by isc::dhcp::ControlledDhcpv4Srv::ControlledDhcpv4Srv(), isc::dhcp::ControlledDhcpv6Srv::ControlledDhcpv6Srv(), isc::dhcp::ControlledDhcpv4Srv::~ControlledDhcpv4Srv(), and isc::dhcp::ControlledDhcpv6Srv::~ControlledDhcpv6Srv().
|
static |
Unparse a parameter map.
params | the parameter map to unparse |
Definition at line 222 of file database_connection.cc.
References isc::data::Element::create(), isc::data::Element::createMap(), isc::db::database_logger, isc::db::DATABASE_TO_JSON_BOOLEAN_ERROR, isc::db::DATABASE_TO_JSON_INTEGER_ERROR, isc::db::DATABASE_TO_JSON_UNKNOWN_TYPE_ERROR, and LOG_ERROR.
Referenced by toElementDbAccessString().
|
static |
Unparse an access string.
dbaccess | the database access string |
Definition at line 279 of file database_connection.cc.
References parse(), and toElement().
Referenced by isc::dhcp::CfgHostDbAccess::toElement(), isc::dhcp::CfgLeaseDbAccess::toElement(), and isc::process::ConfigDbInfo::toElement().
|
protected |
The callback used to recover the connection.
Definition at line 316 of file database_connection.h.
Referenced by isc::db::MySqlConnection::startRecoverDbConnection(), and isc::db::PgSqlConnection::startRecoverDbConnection().
|
static |
Optional callback function to invoke if an opened connection recovery failed.
Definition at line 254 of file database_connection.h.
Referenced by isc::dhcp::ControlledDhcpv4Srv::~ControlledDhcpv4Srv(), isc::dhcp::ControlledDhcpv6Srv::~ControlledDhcpv6Srv(), invokeDbFailedCallback(), isc::dhcp::ControlledDhcpv4Srv::processConfig(), and isc::dhcp::ControlledDhcpv6Srv::processConfig().
|
static |
Optional callback function to invoke if an opened connection is lost.
Definition at line 246 of file database_connection.h.
Referenced by isc::dhcp::ControlledDhcpv4Srv::~ControlledDhcpv4Srv(), isc::dhcp::ControlledDhcpv6Srv::~ControlledDhcpv6Srv(), invokeDbLostCallback(), isc::dhcp::ControlledDhcpv4Srv::processConfig(), and isc::dhcp::ControlledDhcpv6Srv::processConfig().
|
static |
Optional callback function to invoke if an opened connection recovery succeeded.
Definition at line 250 of file database_connection.h.
Referenced by isc::dhcp::ControlledDhcpv4Srv::~ControlledDhcpv4Srv(), isc::dhcp::ControlledDhcpv6Srv::~ControlledDhcpv6Srv(), invokeDbRecoveredCallback(), isc::dhcp::ControlledDhcpv4Srv::processConfig(), and isc::dhcp::ControlledDhcpv6Srv::processConfig().
|
static |
Defines maximum value for time that can be reliably stored.
If I'm still alive I'll be too old to care. You fix it.
Definition at line 134 of file database_connection.h.
Referenced by isc::db::PsqlBindArray::addTimestamp(), isc::db::MySqlBinding::convertToDatabaseTime(), and isc::db::PgSqlExchange::convertToDatabaseTime().
|
static |
Flag which indicates if the database connection should be retried on fail.
Allow the first database connection attempt to fail and start recovery. Sequential tries invoked by the dbReconnect callback should not start yet another database connection attempt.
Definition at line 262 of file database_connection.h.
Referenced by isc::db::DbConnectionInitWithRetry::DbConnectionInitWithRetry(), isc::db::DbConnectionInitWithRetry::~DbConnectionInitWithRetry(), and isc::db::MySqlConnection::openDatabase().
|
static |
Test mode flag (default false).
Definition at line 280 of file database_connection.h.
Referenced by isc::db::DatabaseConnection::EnterTest::EnterTest(), isc::db::DatabaseConnection::EnterTest::~EnterTest(), isc::db::MySqlConnection::ensureSchemaVersion(), and isc::db::PgSqlConnection::ensureSchemaVersion().