2014-04-08 06:51:53 +00:00
|
|
|
|
#pragma once
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
|
|
|
|
#include <list>
|
2015-10-01 14:22:01 +00:00
|
|
|
|
#include <memory>
|
2017-01-13 21:24:59 +00:00
|
|
|
|
#include <mutex>
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
|
|
|
|
#include <Poco/Exception.h>
|
|
|
|
|
#include <mysqlxx/Connection.h>
|
|
|
|
|
|
|
|
|
|
|
2011-03-17 20:00:04 +00:00
|
|
|
|
#define MYSQLXX_POOL_DEFAULT_START_CONNECTIONS 1
|
|
|
|
|
#define MYSQLXX_POOL_DEFAULT_MAX_CONNECTIONS 16
|
|
|
|
|
#define MYSQLXX_POOL_SLEEP_ON_CONNECT_FAIL 10
|
|
|
|
|
|
|
|
|
|
|
2011-03-04 20:58:19 +00:00
|
|
|
|
namespace mysqlxx
|
|
|
|
|
{
|
|
|
|
|
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Пул соединений с MySQL.
|
2011-11-17 19:28:50 +00:00
|
|
|
|
* Этот класс имеет мало отношения к mysqlxx и сделан не в стиле библиотеки. (взят из старого кода)
|
2011-03-18 20:26:54 +00:00
|
|
|
|
* Использование:
|
|
|
|
|
* mysqlxx::Pool pool("mysql_params");
|
|
|
|
|
*
|
|
|
|
|
* void thread()
|
|
|
|
|
* {
|
|
|
|
|
* mysqlxx::Pool::Entry connection = pool.Get();
|
|
|
|
|
* std::string s = connection->query("SELECT 'Hello, world!' AS world").use().fetch()["world"].getString();
|
|
|
|
|
* }
|
2014-06-03 14:32:04 +00:00
|
|
|
|
*
|
|
|
|
|
* TODO: Упростить, используя PoolBase.
|
2011-03-18 20:26:54 +00:00
|
|
|
|
*/
|
2015-02-05 18:47:45 +00:00
|
|
|
|
class Pool final
|
2011-03-04 20:58:19 +00:00
|
|
|
|
{
|
|
|
|
|
protected:
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Информация о соединении. */
|
2011-03-04 20:58:19 +00:00
|
|
|
|
struct Connection
|
|
|
|
|
{
|
2011-10-05 20:21:18 +00:00
|
|
|
|
mysqlxx::Connection conn;
|
2017-01-13 20:37:37 +00:00
|
|
|
|
int ref_count = 0;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Соединение с базой данных. */
|
2011-03-04 20:58:19 +00:00
|
|
|
|
class Entry
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-04-08 07:31:51 +00:00
|
|
|
|
Entry() {}
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
|
|
|
|
Entry(const Entry & src)
|
2011-10-05 20:21:18 +00:00
|
|
|
|
: data(src.data), pool(src.pool)
|
2011-03-04 20:58:19 +00:00
|
|
|
|
{
|
2011-10-10 20:32:42 +00:00
|
|
|
|
incrementRefCount();
|
2011-03-04 20:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-06 15:26:48 +00:00
|
|
|
|
~Entry()
|
2011-03-04 20:58:19 +00:00
|
|
|
|
{
|
2011-10-10 20:32:42 +00:00
|
|
|
|
decrementRefCount();
|
2011-03-04 20:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Entry & operator= (const Entry & src)
|
|
|
|
|
{
|
|
|
|
|
pool = src.pool;
|
2011-10-05 20:21:18 +00:00
|
|
|
|
if (data)
|
2011-10-10 20:32:42 +00:00
|
|
|
|
decrementRefCount();
|
2011-10-05 20:21:18 +00:00
|
|
|
|
data = src.data;
|
|
|
|
|
if (data)
|
2011-10-10 20:32:42 +00:00
|
|
|
|
incrementRefCount();
|
2011-03-04 20:58:19 +00:00
|
|
|
|
return * this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-28 19:11:13 +00:00
|
|
|
|
bool isNull() const
|
|
|
|
|
{
|
2014-04-08 07:31:51 +00:00
|
|
|
|
return data == nullptr;
|
2011-09-28 19:11:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-04 20:58:19 +00:00
|
|
|
|
operator mysqlxx::Connection & ()
|
|
|
|
|
{
|
2011-10-06 15:58:20 +00:00
|
|
|
|
forceConnected();
|
2011-10-05 20:21:18 +00:00
|
|
|
|
return data->conn;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operator const mysqlxx::Connection & () const
|
|
|
|
|
{
|
2011-10-06 15:58:20 +00:00
|
|
|
|
forceConnected();
|
2011-10-05 20:21:18 +00:00
|
|
|
|
return data->conn;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mysqlxx::Connection * operator->() const
|
|
|
|
|
{
|
2011-10-06 15:58:20 +00:00
|
|
|
|
forceConnected();
|
2011-10-05 20:21:18 +00:00
|
|
|
|
return &data->conn;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mysqlxx::Connection * operator->()
|
|
|
|
|
{
|
2011-10-06 15:58:20 +00:00
|
|
|
|
forceConnected();
|
2011-10-05 20:21:18 +00:00
|
|
|
|
return &data->conn;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-05 20:21:18 +00:00
|
|
|
|
Entry(Pool::Connection * conn, Pool * p)
|
|
|
|
|
: data(conn), pool(p)
|
2011-03-04 20:58:19 +00:00
|
|
|
|
{
|
2011-10-10 20:32:42 +00:00
|
|
|
|
incrementRefCount();
|
2011-03-04 20:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-29 09:27:10 +00:00
|
|
|
|
std::string getDescription() const
|
|
|
|
|
{
|
|
|
|
|
if (pool)
|
|
|
|
|
return pool->getDescription();
|
|
|
|
|
else
|
|
|
|
|
return "pool is null";
|
|
|
|
|
}
|
2017-01-13 20:37:37 +00:00
|
|
|
|
|
2011-03-04 20:58:19 +00:00
|
|
|
|
friend class Pool;
|
|
|
|
|
|
|
|
|
|
private:
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Указатель на соединение. */
|
2014-04-08 07:31:51 +00:00
|
|
|
|
Connection * data = nullptr;
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Указатель на пул, которому мы принадлежим. */
|
2014-04-08 07:31:51 +00:00
|
|
|
|
Pool * pool = nullptr;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
2011-09-28 19:11:13 +00:00
|
|
|
|
/** Переподключается к базе данных в случае необходимости. Если не удалось - подождать и попробовать снова. */
|
2017-01-13 21:24:59 +00:00
|
|
|
|
void forceConnected() const;
|
2011-09-28 19:11:13 +00:00
|
|
|
|
|
|
|
|
|
/** Переподключается к базе данных в случае необходимости. Если не удалось - вернуть false. */
|
|
|
|
|
bool tryForceConnected() const
|
|
|
|
|
{
|
2011-10-05 20:21:18 +00:00
|
|
|
|
return data->conn.ping();
|
2011-09-28 19:11:13 +00:00
|
|
|
|
}
|
2011-10-10 20:32:42 +00:00
|
|
|
|
|
2017-01-13 20:37:37 +00:00
|
|
|
|
void incrementRefCount();
|
|
|
|
|
void decrementRefCount();
|
2011-03-04 20:58:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-10-29 09:27:10 +00:00
|
|
|
|
|
2015-02-05 18:47:45 +00:00
|
|
|
|
Pool(const std::string & config_name,
|
|
|
|
|
unsigned default_connections_ = MYSQLXX_POOL_DEFAULT_START_CONNECTIONS,
|
|
|
|
|
unsigned max_connections_ = MYSQLXX_POOL_DEFAULT_MAX_CONNECTIONS,
|
|
|
|
|
const char * parent_config_name_ = nullptr)
|
2017-01-13 21:18:07 +00:00
|
|
|
|
: Pool{Poco::Util::Application::instance().config(), config_name,
|
|
|
|
|
default_connections_, max_connections_, parent_config_name_}
|
2015-02-05 18:47:45 +00:00
|
|
|
|
{}
|
|
|
|
|
|
2011-03-04 20:58:19 +00:00
|
|
|
|
/**
|
2013-04-04 14:39:59 +00:00
|
|
|
|
* @param config_name Имя параметра в конфигурационном файле
|
|
|
|
|
* @param default_connections_ Количество подключений по-умолчанию
|
|
|
|
|
* @param max_connections_ Максимальное количество подключений
|
2011-03-04 20:58:19 +00:00
|
|
|
|
*/
|
2015-02-05 18:47:45 +00:00
|
|
|
|
Pool(const Poco::Util::AbstractConfiguration & cfg, const std::string & config_name,
|
2011-10-06 15:26:48 +00:00
|
|
|
|
unsigned default_connections_ = MYSQLXX_POOL_DEFAULT_START_CONNECTIONS,
|
2013-09-04 15:29:02 +00:00
|
|
|
|
unsigned max_connections_ = MYSQLXX_POOL_DEFAULT_MAX_CONNECTIONS,
|
2017-01-13 21:18:07 +00:00
|
|
|
|
const char * parent_config_name_ = nullptr);
|
2015-10-01 14:22:01 +00:00
|
|
|
|
|
2013-04-04 14:39:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param db_ Имя БД
|
|
|
|
|
* @param server_ Хост для подключения
|
|
|
|
|
* @param user_ Имя пользователя
|
|
|
|
|
* @param password_ Пароль
|
|
|
|
|
* @param port_ Порт для подключения
|
|
|
|
|
* @param default_connections_ Количество подключений по-умолчанию
|
|
|
|
|
* @param max_connections_ Максимальное количество подключений
|
|
|
|
|
*/
|
|
|
|
|
Pool(const std::string & db_,
|
|
|
|
|
const std::string & server_,
|
|
|
|
|
const std::string & user_ = "",
|
|
|
|
|
const std::string & password_ = "",
|
|
|
|
|
unsigned port_ = 0,
|
2013-04-11 17:51:14 +00:00
|
|
|
|
unsigned connect_timeout_ = MYSQLXX_DEFAULT_TIMEOUT,
|
2013-04-24 21:50:19 +00:00
|
|
|
|
unsigned rw_timeout_ = MYSQLXX_DEFAULT_RW_TIMEOUT,
|
2013-04-04 14:39:59 +00:00
|
|
|
|
unsigned default_connections_ = MYSQLXX_POOL_DEFAULT_START_CONNECTIONS,
|
2013-04-11 17:51:14 +00:00
|
|
|
|
unsigned max_connections_ = MYSQLXX_POOL_DEFAULT_MAX_CONNECTIONS)
|
|
|
|
|
: default_connections(default_connections_), max_connections(max_connections_),
|
2015-02-05 18:47:45 +00:00
|
|
|
|
db(db_), server(server_), user(user_), password(password_), port(port_),
|
|
|
|
|
connect_timeout(connect_timeout_), rw_timeout(rw_timeout_) {}
|
|
|
|
|
|
|
|
|
|
Pool(const Pool & other)
|
|
|
|
|
: default_connections{other.default_connections},
|
|
|
|
|
max_connections{other.max_connections},
|
|
|
|
|
db{other.db}, server{other.server},
|
|
|
|
|
user{other.user}, password{other.password},
|
|
|
|
|
port{other.port}, connect_timeout{other.connect_timeout},
|
|
|
|
|
rw_timeout{other.rw_timeout}
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
Pool & operator=(const Pool &) = delete;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
2017-01-13 20:37:37 +00:00
|
|
|
|
~Pool();
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Выделяет соединение для работы. */
|
2017-01-13 20:37:37 +00:00
|
|
|
|
Entry Get();
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
2011-09-28 19:11:13 +00:00
|
|
|
|
/** Выделяет соединение для работы.
|
|
|
|
|
* Если база недоступна - возвращает пустой объект Entry.
|
|
|
|
|
* Если пул переполнен - кидает исключение.
|
|
|
|
|
*/
|
2017-01-13 20:37:37 +00:00
|
|
|
|
Entry tryGet();
|
2011-09-28 19:11:13 +00:00
|
|
|
|
|
|
|
|
|
/// Получить описание БД
|
|
|
|
|
std::string getDescription() const
|
|
|
|
|
{
|
2011-10-05 20:21:18 +00:00
|
|
|
|
return description;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Количество соединений с MySQL, создаваемых при запуске. */
|
|
|
|
|
unsigned default_connections;
|
|
|
|
|
/** Максимально возможное количество соедиений. */
|
|
|
|
|
unsigned max_connections;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Признак того, что мы инициализированы. */
|
2015-02-05 18:47:45 +00:00
|
|
|
|
bool initialized{false};
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Список соединений. */
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using Connections = std::list<Connection *>;
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Список соединений. */
|
|
|
|
|
Connections connections;
|
|
|
|
|
/** Замок для доступа к списку соединений. */
|
2017-01-13 21:24:59 +00:00
|
|
|
|
std::mutex mutex;
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Описание соединения. */
|
2011-10-05 20:21:18 +00:00
|
|
|
|
std::string description;
|
2015-10-01 14:22:01 +00:00
|
|
|
|
|
2013-04-04 14:39:59 +00:00
|
|
|
|
/** Параметры подключения. **/
|
|
|
|
|
std::string db;
|
|
|
|
|
std::string server;
|
|
|
|
|
std::string user;
|
|
|
|
|
std::string password;
|
|
|
|
|
unsigned port;
|
2013-04-11 17:51:14 +00:00
|
|
|
|
unsigned connect_timeout;
|
|
|
|
|
unsigned rw_timeout;
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Хотя бы один раз было успешное соединение. */
|
2015-02-05 18:47:45 +00:00
|
|
|
|
bool was_successful{false};
|
2011-09-01 18:15:15 +00:00
|
|
|
|
|
2011-10-06 15:26:48 +00:00
|
|
|
|
/** Выполняет инициализацию класса, если мы еще не инициализированы. */
|
2017-01-13 20:37:37 +00:00
|
|
|
|
void initialize();
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
2017-01-13 20:37:37 +00:00
|
|
|
|
/** Create new connection. */
|
|
|
|
|
Connection * allocConnection(bool dont_throw_if_failed_first_time = false);
|
2011-03-04 20:58:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|