20 #include "../../../lib/repo_stack.h"
21 #include "../../../lib/repo_log.h"
23 #if defined(_WIN32) || defined(_WIN64)
27 #define strcasecmp _stricmp
30 #include <mongo/client/dbclient.h>
35 namespace connectionPool{
44 const int &numConnections,
45 mongo::ConnectionString dbAddress,
46 mongo::BSONObj* auth) :
47 maxSize(numConnections),
49 auth(auth? new mongo::BSONObj(*auth) : nullptr)
51 repoDebug <<
"Instantiating Mongo connection pool with " << maxSize <<
" connections...";
56 mongo::DBClientBase *worker = dbAddress.connect(errMsg);
60 repoDebug <<
"Connected to database, trying authentication..";
61 for (
int i = 0; i < numConnections; i++)
63 mongo::DBClientBase *worker = dbAddress.connect(errMsg);
66 repoTrace << auth->toString();
67 if (!worker->auth(auth->getStringField(
"db"), auth->getStringField(
"user"), auth->getStringField(
"pwd"), errMsg, auth->getField(
"digestPassword").boolean()))
69 throw mongo::DBException(errMsg, mongo::ErrorCodes::AuthenticationFailed);
74 repoWarning <<
"No credentials found. User is not authenticated against the database!";
81 repoDebug <<
"Failed to connect: " << errMsg;
82 throw mongo::DBException(errMsg, 1000);
88 ~MongoConnectionPool()
92 std::vector<mongo::DBClientBase*> workers =
empty();
93 std::vector<mongo::DBClientBase*>::iterator it;
94 for (it = workers.begin(); it != workers.end(); ++it)
96 mongo::DBClientBase* worker = *it;
102 mongo::DBClientBase* getWorker()
107 void returnWorker(mongo::DBClientBase *worker)
112 mongo::DBClientBase* pop()
114 mongo::DBClientBase* worker = RepoStack::pop();
120 worker = connectWorker(tmp);
125 if (!worker->isStillConnected())
128 worker = connectWorker(tmp);
135 void push(mongo::DBClientBase *worker)
138 RepoStack::push(worker);
141 mongo::DBClientBase* connectWorker(std::string &errMsg)
143 mongo::DBClientBase *worker = dbAddress.connect(errMsg);
144 if (auth && !worker->auth(auth->getStringField(
"db"), auth->getStringField(
"user"), auth->getStringField(
"pwd"), errMsg, auth->getField(
"digestPassword").boolean()))
146 throw mongo::DBException(errMsg, mongo::ErrorCodes::AuthenticationFailed);
151 const uint32_t maxSize;
152 const mongo::ConnectionString dbAddress;
153 const mongo::BSONObj *auth;
Definition: repo_connection_pool_mongo.h:36
Definition: repo_connection_pool_mongo.h:32
std::vector< mongo::DBClientBase * > empty()
Definition: repo_stack.h:64
MongoConnectionPool(const int &numConnections, mongo::ConnectionString dbAddress, mongo::BSONObj *auth)
Definition: repo_connection_pool_mongo.h:43
Definition: repo_stack.h:32