Library: MongoDB
Package: MongoDB
Header: Poco/MongoDB/ReplicaSetConnection.h
Description
Wrapper around Connection that provides automatic retry and failover for MongoDB replica set operations.
This class wraps a Connection and automatically retries failed operations on different replica set members. It detects retriable errors (network failures, "not master" errors, etc.) and seamlessly fails over to another suitable server.
Usage example:
ReplicaSet rs(config);
ReplicaSetConnection::Ptr conn = new ReplicaSetConnection(rs, ReadPreference::Primary);
OpMsgMessage request("mydb", "mycollection");
request.setCommandName(OpMsgMessage::CMD_FIND);
request.body().add("filter", filterDoc);
OpMsgMessage response;
conn->sendRequest(request, response); // Automatic retry on failure
THREAD SAFETY: This class is NOT thread-safe, just like Connection. Each thread must have its own ReplicaSetConnection instance, or use connection pooling with external synchronization.
For multi-threaded applications, use ReplicaSetPoolableConnectionFactory with Poco::ObjectPool.
Member Summary
Member Functions: address, connection, isConnected, matchesReadPreference, readResponse, reconnect, sendRequest
Types Aliases
Ptr
using Ptr = Poco::SharedPtr < ReplicaSetConnection >;
Constructors
ReplicaSetConnection
ReplicaSetConnection(
ReplicaSet & replicaSet,
const ReadPreference & readPref
);
Creates a ReplicaSetConnection for the given replica set and read preference. The connection is established lazily on first use.
Destructor
~ReplicaSetConnection
Destroys the ReplicaSetConnection.
Member Functions
address
[[nodiscard]]
Net::SocketAddress address() const;
Returns the address of the currently connected server. Throws Poco::NullPointerException if not connected.
connection
[[nodiscard]]
Connection & connection();
Returns a reference to the underlying Connection. Throws Poco::NullPointerException if not connected.
isConnected
[[nodiscard]]
bool isConnected() const noexcept;
Returns true if currently connected to a server.
matchesReadPreference
[[nodiscard]]
bool matchesReadPreference() const noexcept;
Returns true if the currently connected server still matches the read preference. Returns false if not connected or if the server no longer satisfies the read preference. This is useful for connection pool validation to detect when a server role has changed (e.g., primary became secondary).
readResponse
void readResponse(
OpMsgMessage & response
);
Reads a response for a previously sent request.
reconnect
void reconnect();
Forces reconnection by selecting a new server from the replica set. Useful if you detect an error and want to explicitly retry.
sendRequest
void sendRequest(
OpMsgMessage & request,
OpMsgMessage & response
);
Sends a request and reads the response. Automatically retries on retriable errors with failover.
Throws Poco::IOException if all retry attempts fail.
sendRequest
void sendRequest(
OpMsgMessage & request
);
Sends a one-way request (fire-and-forget). Sets MSG_MORE_TO_COME flag and acknowledged=false.
Note: One-way requests are not retried on failure.