From b05c968bb280351f594eae5483c65434ce671022 Mon Sep 17 00:00:00 2001 From: Frank Park Date: Sun, 26 Mar 2017 21:52:33 -0400 Subject: [PATCH] add is_connection_compatible along with some minor changes --- include/sqlpp11/connection_pool.h | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/include/sqlpp11/connection_pool.h b/include/sqlpp11/connection_pool.h index 571eee26..d81e8c89 100644 --- a/include/sqlpp11/connection_pool.h +++ b/include/sqlpp11/connection_pool.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013 - 2016, Roland Bock +* Copyright (c) 2013 - 2017, Roland Bock, Frank Park * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -30,6 +30,8 @@ #include #include #include +#include +#include namespace sqlpp { @@ -39,12 +41,14 @@ namespace sqlpp private: std::mutex connection_pool_mutex; const std::shared_ptr config; - unsigned int default_pool_size = 0; + unsigned int maximum_pool_size = 0; std::stack> free_connections; + } + public: connection_pool(const std::shared_ptr& config, unsigned int pool_size) - : config(config), default_pool_size(pool_size) + : config(config), maximum_pool_size(pool_size) { std::lock_guard lock(connection_pool_mutex); try @@ -56,8 +60,8 @@ namespace sqlpp } catch (const sqlpp::exception& e) { - std::cerr << "Failed to spawn new connection." << std::endl; - std::cerr << e.what() << std::endl; + std::cerr << "Failed to spawn a new connection." << std::endl; + throw; } } ~connection_pool() = default; @@ -83,9 +87,8 @@ namespace sqlpp } catch (const sqlpp::exception& e) { - std::cerr << "Failed to spawn new connection." << std::endl; - std::cerr << e.what() << std::endl; - return std::unique_ptr(); + std::cerr << "Failed to spawn a new connection." << std::endl; + throw; } } @@ -94,7 +97,7 @@ namespace sqlpp void free_connection(std::unique_ptr connection) { std::lock_guard lock(connection_pool_mutex); - if (free_connections.size() >= default_pool_size) + if (free_connections.size() >= maximum_pool_size) { // Exceeds default size, do nothing and let unique_ptr self destroy. } @@ -102,7 +105,13 @@ namespace sqlpp { if (connection.get()) { - free_connections.push(std::move(connection)); + { + free_connections.push(std::move(connection)); + } + else + { + throw sqlpp::exception("Trying to free a connection with incompatible config."); + } } else {