Docs Menu
Docs Home
/ / /
C++ Driver
/

Connection Pools

On this page

  • mongocxx::client vs mongocxx::pool
  • Using Threads with Connection Pools
  • Using Forks with Connection Pools
  • Configuring a Connection Pool
  • Using a Connection Pool

A standalone mongocxx::client uses a single-threaded algorithm to monitor the state of the cluster it's connected to. When connected to a replica set, the thread "stops the world" every 60 seconds to check the status of the cluster. A mongocxx::pool, on the other hand, uses a separate background thread for each server in the cluster, each of which checks the status of the server it monitors every 10 seconds. Because of the performance advantages of monitoring the cluster in the background rather than "stopping the world", it's highly recommended to use a mongocxx::pool rather than a set of standalone clients if your application has access to multiple threads, even if your application only uses one thread.

A mongocxx::pool can be shared across multiple threads and used to create clients. However, each mongocxx::client can only be used in a single thread. See the thread safety documentation for details on how to use a mongocxx::client in a thread-safe manner.

A mongocxx::pool cannot be shared between a parent and a fork. You must create your connection pool after forking. See the fork safety documentation documentation.

The number of clients in a connection pool is limited by the URI parameter maxPoolSize. After the number of clients created by a mongocxx::pool (both in the pool and checked out) reaches the value of maxPoolSize, mongocxx::pool::acquire blocks until another thread returns a client to the pool. The default value is 100.

To use a connection pool, first create a mongocxx::pool, passing the URI as an argument. Then, call mongocxx::pool::acquire to receive a client from the pool. The client will automatically be returned to the pool when it goes out of scope.

See the connection pool example for more details.

Back

Stable API