Commit 2c139644 authored by Steven Murray's avatar Steven Murray
Browse files

ConnPool::getConn() now throws an exception if the pool was configured with zero connections

parent 0d76da86
Pipeline #13759 failed with stages
in 7 minutes and 2 seconds
...@@ -42,6 +42,11 @@ ConnPool::ConnPool(const Login &login, const uint64_t maxNbConns): ...@@ -42,6 +42,11 @@ ConnPool::ConnPool(const Login &login, const uint64_t maxNbConns):
Conn ConnPool::getConn() { Conn ConnPool::getConn() {
threading::MutexLocker locker(m_connsAndStmtsMutex); threading::MutexLocker locker(m_connsAndStmtsMutex);
if(0 == m_maxNbConns) {
throw ConnPoolConfiguredWithZeroConns(std::string(__FUNCTION__) +
" failed: ConnPool is configured with zero connections");
}
while(m_connsAndStmts.size() == 0 && m_nbConnsOnLoan == m_maxNbConns) { while(m_connsAndStmts.size() == 0 && m_nbConnsOnLoan == m_maxNbConns) {
m_connsAndStmtsCv.wait(locker); m_connsAndStmtsCv.wait(locker);
} }
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#pragma once #pragma once
#include "common/exception/Exception.hpp"
#include "common/threading/CondVar.hpp" #include "common/threading/CondVar.hpp"
#include "common/threading/Mutex.hpp" #include "common/threading/Mutex.hpp"
#include "rdbms/ConnAndStmts.hpp" #include "rdbms/ConnAndStmts.hpp"
...@@ -49,14 +50,20 @@ public: ...@@ -49,14 +50,20 @@ public:
*/ */
ConnPool(const Login &login, const uint64_t maxNbConns); ConnPool(const Login &login, const uint64_t maxNbConns);
CTA_GENERATE_EXCEPTION_CLASS(ConnPoolConfiguredWithZeroConns);
/** /**
* Takes a connection from the pool. * Takes a connection from the pool.
* *
* Please note that this method will block if the pool is empty. In such a * Please note that this method will block if the pool is empty. In such a
* situation this method will unblock when a connection is returned to the * situation this method will unblock when a connection is returned to the
* pool. * pool. There is one exception to this rule. If the pool was configured
* with maxNbConns set to 0 then calling this method with throw a
* ConnPoolConfiguredWithZeroConns exception.
* *
* @return A connection from the pool. * @return A connection from the pool.
* @throw ConnPoolConfiguredWithZeroConns If this pool was configured with
* maxNbConns set to 0.
*/ */
Conn getConn(); Conn getConn();
......
...@@ -38,18 +38,28 @@ TEST_F(cta_rdbms_ConnPoolTest, getPooledConn) { ...@@ -38,18 +38,28 @@ TEST_F(cta_rdbms_ConnPoolTest, getPooledConn) {
using namespace cta::rdbms; using namespace cta::rdbms;
const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0); const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0);
const uint64_t nbConns = 2; const uint64_t maxNbConns = 2;
ConnPool pool(login, nbConns); ConnPool pool(login, maxNbConns);
Conn conn = pool.getConn(); Conn conn = pool.getConn();
} }
TEST_F(cta_rdbms_ConnPoolTest, getPooledConn_maxNbConns_zero) {
using namespace cta::rdbms;
const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0);
const uint64_t maxNbConns = 0;
ConnPool pool(login, maxNbConns);
ASSERT_THROW(Conn conn = pool.getConn(), ConnPool::ConnPoolConfiguredWithZeroConns);
}
TEST_F(cta_rdbms_ConnPoolTest, assignment) { TEST_F(cta_rdbms_ConnPoolTest, assignment) {
using namespace cta::rdbms; using namespace cta::rdbms;
const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0); const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0);
const uint64_t nbConns = 2; const uint64_t maxNbConns = 2;
ConnPool pool(login, nbConns); ConnPool pool(login, maxNbConns);
Conn conn = pool.getConn(); Conn conn = pool.getConn();
...@@ -62,8 +72,8 @@ TEST_F(cta_rdbms_ConnPoolTest, moveConstructor) { ...@@ -62,8 +72,8 @@ TEST_F(cta_rdbms_ConnPoolTest, moveConstructor) {
using namespace cta::rdbms; using namespace cta::rdbms;
const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0); const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0);
const uint64_t nbConns = 2; const uint64_t maxNbConns = 2;
ConnPool pool(login, nbConns); ConnPool pool(login, maxNbConns);
Conn conn = pool.getConn(); Conn conn = pool.getConn();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment