#pragma once #include #include namespace DB { using Poco::SharedPtr; /** Интерфейс для пулов соединений. * * Использование (на примере обычного ConnectionPool): * ConnectionPool pool(...); * * void thread() * { * sqxxl::Pool::Entry connection = pool.get(); * connection->sendQuery("SELECT 'Hello, world!' AS world"); * } */ class IConnectionPool : private boost::noncopyable { public: typedef PoolBase::Entry Entry; virtual Entry get(Settings * settings = nullptr) = 0; virtual ~IConnectionPool() {} }; typedef SharedPtr ConnectionPoolPtr; typedef std::vector ConnectionPools; /** Обычный пул соединений, без отказоустойчивости. */ class ConnectionPool : public PoolBase, public IConnectionPool { public: typedef IConnectionPool::Entry Entry; typedef PoolBase Base; ConnectionPool(unsigned max_connections_, const String & host_, UInt16 port_, const String & default_database_, const String & user_, const String & password_, const DataTypeFactory & data_type_factory_, const String & client_name_ = "client", Protocol::Compression::Enum compression_ = Protocol::Compression::Enable, Poco::Timespan connect_timeout_ = Poco::Timespan(DBMS_DEFAULT_CONNECT_TIMEOUT_SEC, 0), Poco::Timespan receive_timeout_ = Poco::Timespan(DBMS_DEFAULT_RECEIVE_TIMEOUT_SEC, 0), Poco::Timespan send_timeout_ = Poco::Timespan(DBMS_DEFAULT_SEND_TIMEOUT_SEC, 0)) : Base(max_connections_, &Logger::get("ConnectionPool (" + Poco::Net::SocketAddress(host, port).toString() + ")")), host(host_), port(port_), default_database(default_database_), user(user_), password(password_), client_name(client_name_), compression(compression_), data_type_factory(data_type_factory_), connect_timeout(connect_timeout_), receive_timeout(receive_timeout_), send_timeout(send_timeout_) { } /** Выделяет соединение для работы. */ Entry get(Settings * settings = nullptr) override { if (settings) return Base::get(settings->queue_max_wait_ms.totalMilliseconds()); else return Base::get(-1); } const std::string & getHost() const { return host; } protected: /** Создает новый объект для помещения в пул. */ ConnectionPtr allocObject() override { return new Connection( host, port, default_database, user, password, data_type_factory, client_name, compression, connect_timeout, receive_timeout, send_timeout); } private: String host; UInt16 port; String default_database; String user; String password; String client_name; Protocol::Compression::Enum compression; /// Сжимать ли данные при взаимодействии с сервером. const DataTypeFactory & data_type_factory; Poco::Timespan connect_timeout; Poco::Timespan receive_timeout; Poco::Timespan send_timeout; }; }