From 0411455ef07747cc08878d549c982c5a0f5f22dc Mon Sep 17 00:00:00 2001 From: Matej Kenda Date: Wed, 3 Dec 2025 10:59:34 +0100 Subject: [PATCH] enh(MongoDB): connect to server in pool activateObject. --- .../MongoDB/ReplicaSetPoolableConnectionFactory.h | 11 ++++++++++- MongoDB/src/Connection.cpp | 14 +++----------- MongoDB/src/ReplicaSetConnection.cpp | 4 ++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/MongoDB/include/Poco/MongoDB/ReplicaSetPoolableConnectionFactory.h b/MongoDB/include/Poco/MongoDB/ReplicaSetPoolableConnectionFactory.h index 47ab3d8e0..e4f65f079 100644 --- a/MongoDB/include/Poco/MongoDB/ReplicaSetPoolableConnectionFactory.h +++ b/MongoDB/include/Poco/MongoDB/ReplicaSetPoolableConnectionFactory.h @@ -70,7 +70,16 @@ public: void activateObject(MongoDB::ReplicaSetConnection::Ptr pObject) { - // No action needed - connection is established lazily + if (!pObject->isConnected()) + { + try { + pObject->reconnect(); + } + catch (Poco::Exception& e) + { + // Ignore connect error. c->isConnected() can be used to determine if the connection is valid. + } + } } void deactivateObject(MongoDB::ReplicaSetConnection::Ptr pObject) diff --git a/MongoDB/src/Connection.cpp b/MongoDB/src/Connection.cpp index e152bdc04..bf0b99ea2 100644 --- a/MongoDB/src/Connection.cpp +++ b/MongoDB/src/Connection.cpp @@ -27,14 +27,10 @@ namespace Poco { namespace MongoDB { -Connection::SocketFactory::SocketFactory() -{ -} +Connection::SocketFactory::SocketFactory() = default; -Connection::SocketFactory::~SocketFactory() -{ -} +Connection::SocketFactory::~SocketFactory() = default; Poco::Net::StreamSocket Connection::SocketFactory::createSocket(const std::string& host, int port, Poco::Timespan connectTimeout, bool secure) @@ -53,11 +49,7 @@ Poco::Net::StreamSocket Connection::SocketFactory::createSocket(const std::strin } -Connection::Connection(): - _address(), - _socket() -{ -} +Connection::Connection() = default; Connection::Connection(const std::string& hostAndPort): diff --git a/MongoDB/src/ReplicaSetConnection.cpp b/MongoDB/src/ReplicaSetConnection.cpp index 8b4c85dc6..1265beef1 100644 --- a/MongoDB/src/ReplicaSetConnection.cpp +++ b/MongoDB/src/ReplicaSetConnection.cpp @@ -86,7 +86,7 @@ Net::SocketAddress ReplicaSetConnection::address() const { if (_connection.isNull()) { - throw Poco::NullPointerException("Not connected to any server"); + throw Poco::NullPointerException("Not connected to any server: address not available."); } return _connection->address(); } @@ -96,7 +96,7 @@ Connection& ReplicaSetConnection::connection() { if (_connection.isNull()) { - throw Poco::NullPointerException("Not connected to any server"); + throw Poco::NullPointerException("Not connected to any server: connection not available."); } return *_connection; }