Skip to content
Snippets Groups Projects
Commit 4dbbe002 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 bf6618d4
Branches
Tags
No related merge requests found
......@@ -42,6 +42,11 @@ ConnPool::ConnPool(const Login &login, const uint64_t maxNbConns):
Conn ConnPool::getConn() {
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) {
m_connsAndStmtsCv.wait(locker);
}
......
......@@ -18,6 +18,7 @@
#pragma once
#include "common/exception/Exception.hpp"
#include "common/threading/CondVar.hpp"
#include "common/threading/Mutex.hpp"
#include "rdbms/ConnAndStmts.hpp"
......@@ -49,14 +50,20 @@ public:
*/
ConnPool(const Login &login, const uint64_t maxNbConns);
CTA_GENERATE_EXCEPTION_CLASS(ConnPoolConfiguredWithZeroConns);
/**
* Takes a connection from the pool.
*
* 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
* 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.
* @throw ConnPoolConfiguredWithZeroConns If this pool was configured with
* maxNbConns set to 0.
*/
Conn getConn();
......
......@@ -38,18 +38,28 @@ TEST_F(cta_rdbms_ConnPoolTest, getPooledConn) {
using namespace cta::rdbms;
const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0);
const uint64_t nbConns = 2;
ConnPool pool(login, nbConns);
const uint64_t maxNbConns = 2;
ConnPool pool(login, maxNbConns);
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) {
using namespace cta::rdbms;
const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0);
const uint64_t nbConns = 2;
ConnPool pool(login, nbConns);
const uint64_t maxNbConns = 2;
ConnPool pool(login, maxNbConns);
Conn conn = pool.getConn();
......@@ -62,8 +72,8 @@ TEST_F(cta_rdbms_ConnPoolTest, moveConstructor) {
using namespace cta::rdbms;
const Login login(Login::DBTYPE_SQLITE, "", "", "file::memory:?cache=shared", "", 0);
const uint64_t nbConns = 2;
ConnPool pool(login, nbConns);
const uint64_t maxNbConns = 2;
ConnPool pool(login, maxNbConns);
Conn conn = pool.getConn();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment