diff --git a/include/sqlpp11/connection_pool.h b/include/sqlpp11/connection_pool.h index b29bb2e5..06594b12 100644 --- a/include/sqlpp11/connection_pool.h +++ b/include/sqlpp11/connection_pool.h @@ -133,8 +133,6 @@ namespace sqlpp typename Connection = typename std::enable_if::value, Connection_config::connection>::type> class connection_pool_t { - friend pool_connection; - private: std::mutex connection_pool_mutex; const std::shared_ptr config; @@ -142,6 +140,55 @@ namespace sqlpp std::stack> free_connections; Connection_validator connection_validator; + public: + connection_pool_t(const std::shared_ptr& config, size_t pool_size) + : config(config), maximum_pool_size(pool_size), connection_validator(Connection_validator()) {} + ~connection_pool_t() = default; + connection_pool_t(const connection_pool_t&) = delete; + connection_pool_t(connection_pool_t&& other) + : config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)), + connection_validator(std::move(other.connection_validator)) {} + connection_pool_t& operator=(const connection_pool_t&) = delete; + connection_pool_t& operator=(connection_pool_t&&) = delete; + + template > + auto get_connection() + -> Pool_connection + { + std::lock_guard lock(connection_pool_mutex); + while (true) + { + try + { + if (!free_connections.empty()) + { + auto connection = std::move(free_connections.top()); + free_connections.pop(); + connection_validator.validate(connection.get()); + + return Pool_connection(std::move(connection), this); + } + else + { + break; + } + } + catch (const sqlpp::exception&) + { + throw sqlpp::exception("Failed to retrieve a valid connection."); + } + } + + try + { + return Pool_connection(std::move(std::make_unique(config)), this); + } + catch (const sqlpp::exception&) + { + throw sqlpp::exception("Failed to spawn a new connection."); + } + } + void free_connection(std::unique_ptr& connection) { std::lock_guard lock(connection_pool_mutex); @@ -163,54 +210,6 @@ namespace sqlpp } } - public: - connection_pool_t(const std::shared_ptr& config, size_t pool_size) - : config(config), maximum_pool_size(pool_size), connection_validator(Connection_validator()) {} - ~connection_pool_t() = default; - connection_pool_t(const connection_pool_t&) = delete; - connection_pool_t(connection_pool_t&& other) - : config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)), - connection_validator(std::move(other.connection_validator)) {} - connection_pool_t& operator=(const connection_pool_t&) = delete; - connection_pool_t& operator=(connection_pool_t&&) = delete; - - auto get_connection() - -> pool_connection - { - std::lock_guard lock(connection_pool_mutex); - while (true) - { - try - { - if (!free_connections.empty()) - { - auto connection = std::move(free_connections.top()); - free_connections.pop(); - connection_validator.validate(connection.get()); - - return pool_connection(std::move(connection), this); - } - else - { - break; - } - } - catch (const sqlpp::exception&) - { - throw sqlpp::exception("Failed to retrieve a valid connection."); - } - } - - try - { - return pool_connection(std::move(std::make_unique(config)), this); - } - catch (const sqlpp::exception&) - { - throw sqlpp::exception("Failed to spawn a new connection."); - } - } - template void operator()(Query query, Lambda callback) { diff --git a/include/sqlpp11/pool_connection.h b/include/sqlpp11/pool_connection.h index 436a5d5d..45123b53 100644 --- a/include/sqlpp11/pool_connection.h +++ b/include/sqlpp11/pool_connection.h @@ -34,8 +34,7 @@ namespace sqlpp { - template > + template struct pool_connection : public sqlpp::connection { private: @@ -90,11 +89,11 @@ namespace sqlpp return _impl->prepare(t); } - template::value || is_invocable::value || - is_invocable::value && - !is_invocable::value, int>::type = 0> + is_invocable::value && + !is_invocable::value, int>::type = 0> void operator()(Query query, Lambda callback) { try @@ -103,12 +102,12 @@ namespace sqlpp } catch (const std::exception& e) { - invoke_callback(sqlpp::exception(sqlpp::exception::query_error, e.what()), pool_connection(), decltype(pool_connection()(Query()))(), callback); + invoke_callback(sqlpp::exception(sqlpp::exception::query_error, e.what()), pool_connection(), Result(), callback); } } - template::value, int>::type = 0> + template::value, int>::type = 0> void operator()(Query query, Lambda callback) { static_assert(false, "Direct query with pool connection forbids callback with parameter of type connection.");