ConnectionManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <memory>
5#include <mutex>
6#include <sstream>
7#include <string>
8
9#include <bsoncxx/builder/stream/array.hpp>
10#include <bsoncxx/builder/stream/document.hpp>
11#include <bsoncxx/builder/stream/helpers.hpp>
12#include <bsoncxx/json.hpp>
13#include <mongocxx/client.hpp>
14#include <mongocxx/instance.hpp>
15#include <mongocxx/pool.hpp>
16#include <mongocxx/stdx.hpp>
17#include <mongocxx/uri.hpp>
18
20{
21
22 using PoolClientPtr = mongocxx::pool::entry;
23
24 /**
25 * @brief A manager of multiple mongodb connection
26 */
28 {
29 public:
31
33 {
34 std::string host = "";
35 unsigned int port = 0;
36 std::string user = "";
37 std::string password = "";
38 std::string database = "Test";
39 int minPoolSize = 5;
40 int maxPoolSize = 100;
41
43
44 bool isSet() const;
45
46 std::string baseUri() const;
47
48 std::string key() const;
49
50 std::string uri() const;
51
52 std::string toString() const;
53 };
54
55 static mongocxx::pool& Connect(const MongoDBSettings& settings);
56 static bool ConnectionIsValid(const MongoDBSettings& settings,
57 bool forceNewConnection = false);
58 static bool ConnectionExists(const MongoDBSettings& settings);
59
60 private:
61 static void initialize_if();
62
63
64 private:
65 static std::mutex initializationMutex;
66 static bool initialized;
67 static std::map<std::string, std::unique_ptr<mongocxx::pool>> Connections;
68 };
69} // namespace armarx::armem::server::ltm::mongodb
static mongocxx::pool & Connect(const MongoDBSettings &settings)
static bool ConnectionIsValid(const MongoDBSettings &settings, bool forceNewConnection=false)
static bool ConnectionExists(const MongoDBSettings &settings)