Learn the "why" behind slow queries and how to fix them in our 2-Part Webinar.
Register now >
Docs Menu
Docs Home
/ /

接続プールの使用

このガイドでは、 C++ドライバーを使用して接続プールを作成および構成する方法を学習できます。接続プールは、アプリケーションが複数の同時MongoDB操作を管理するために使用できるクライアントオブジェクトのキャッシュです。

mongocxx::pool をインスタンス化すると、プールは最大プール サイズに達するまで必要に応じてクライアントオブジェクトを作成します。 MongoDB操作を実行する各スレッドは、プールからクライアントをチェックアウトし、そのプールを使用して操作を実行し、クライアントをプールに返します。プールは、これらのクライアントのライフサイクルを自動的に管理します。

重要

ほとんどのMongoDBドライバーとは異なり、 C++ドライバーはMongoDB 接続監視とPooling(CMAP) 仕様を実装していません。このコンテキストでは、接続プーリングとは、データベース接続ではなく、クライアントオブジェクトのドライバーのキャッシュを指します。

接続プールは、スタンドアロンクライアントと比べてパフォーマンス上の大きな利点を提供します。 mongocxx::pool は、個別のバックグラウンド モニタリング スレッドを使用してクラスターを継続的に監視します。その結果、アプリケーションスレッドはトポロジーモニタリングの完了を待たずに操作を実行できるようになります。対照的に、スタンドアロンのmongocxx::client では定期的にクラスターのステータスを確認するために停止し、チェック中はすべての操作がブロックされます。

アプリケーションが1 つのスレッドしか使用しない場合でも、接続プールを使用できます。プールのバックグラウンド モニタリングは、スタンドアロンクライアントの定期的なチェックよりもパフォーマンスが優れています。

次の表には、mongocxx::poolmongocxx::client の主な違いをまとめています。

機能
mongocxx::pool
mongocxx::client

マルチスレッド アプリケーションに推奨

はい。

No.

バックグラウンド モニタリング

はい。プールは 各サーバーに対して個別のスレッドを使用します。

いいえ。クライアントは定期的にブロッキングチェックを実行します。

モニタリング間隔

サーバーごとの 10 秒ごと。

60 秒ごと。

スレッドの安全性

スレッドセーフですが、取得されたクライアントはスレッドセーフではありません。

スレッドセーフではありません。

フォークの安全性

フォーク後にプールを作成する必要があります。

フォーク後にクライアントを作成する必要があります。

Tip

スレッドとフォーク

クライアントとクライアントオブジェクトを複数のスレッド間で使用する方法や、プロセスをフォークする方法の詳細については、「 スレッドとフォーク安全性ガイド 」を参照してください。

接続プールを作成するには、接続 URI をそのコンストラクターに渡して mongocxx::poolオブジェクトをインスタンス化します。 MongoDB操作を実行する各スレッドは、acquire() メンバー関数を呼び出してプールからクライアントを取得する必要があります。クライアントが範囲を超えると、自動的に プールに戻ります。

このサンプルファイルは、次のアクションを実行します。

  • 接続プールを作成する

  • プールからそれぞれが独自のクライアントを取得する 3 つのスレッドを作成します

  • 各スレッドで挿入操作を実行する

1#include <iostream>
2#include <thread>
3#include <vector>
4
5#include <bsoncxx/builder/basic/document.hpp>
6#include <bsoncxx/builder/basic/kvp.hpp>
7#include <mongocxx/instance.hpp>
8#include <mongocxx/pool.hpp>
9#include <mongocxx/uri.hpp>
10
11using bsoncxx::builder::basic::kvp;
12using bsoncxx::builder::basic::make_document;
13
14int main() {
15 mongocxx::instance instance{};
16
17 mongocxx::uri uri{"mongodb://localhost:27017"};
18 mongocxx::pool pool{uri};
19
20 std::vector<std::thread> threads;
21
22 // Creates multiple threads that each acquire a client from the pool
23 for (int i = 0; i < 3; ++i) {
24 threads.emplace_back([&pool, i]() {
25 auto client = pool.acquire();
26 auto collection = (*client)["db"]["coll"];
27
28 // Inserts a sample document
29 collection.insert_one(make_document(kvp("thread_id", i)));
30
31 std::cout << "Thread " << i << " inserted a document" << std::endl;
32 });
33 }
34
35 // Waits for all threads to complete
36 for (auto& thread : threads) {
37 thread.join();
38 }
39
40 std::cout << "All threads completed" << std::endl;
41
42 return 0;
43}

接続 URI にオプションを含めることで、接続プールの動作を構成できます。次の表では、設定できる接続プールオプションについて説明しています。

URI オプション
説明

maxPoolSize

Specifies the maximum number of clients the pool can create. When this limit is reached, the acquire() member function blocks until another thread returns a client to the pool.
Defaults to 100.
Type: int

minPoolSize

Specifies the minimum number of clients the pool maintains. The pool creates this number of clients when initialized and maintains at least this number of clients continuously.
Defaults to 0.
Type: int

waitQueueTimeoutMS

Specifies the maximum time in milliseconds that the acquire() function waits for a client to become available when the pool has reached the maxPoolSize value. If this timeout expires, the acquire() function generates an error.
Defaults to no timeout.
Type: int

次の例では、 maxPoolSize オプションを指定して、最大サイズが 5 クライアントの接続プールを作成します。

mongocxx::uri uri{"mongodb://localhost:27017/?maxPoolSize=5"};
mongocxx::pool pool{uri};

このガイドでは型と関数について詳しくは、次のAPIドキュメントを参照してください。

戻る

Stable API

項目一覧