MongoNotPrimaryException (not master)

I am connecting to mongodb replicaset where I have write concern = 2 and readPreference is PrimaryOnly. I am seeing a lot of this exceptions currently.

Questions.

  1. When I connect to a mongodb do I need to give the hostnames of all the mongo servers or just 1 is enough ?

  2. This exception is coming for a write operation will it also come for read operation

Here is the exception

com.mongodb.MongoNotPrimaryException: Command failed with error 10107 (NotMaster): 'not master' on server xxx.hostname:port . The full response is {"operationTime": {"$timestamp": {"t": 1595902580, "i": 1}}, "ok": 0.0, "errmsg": "not master", "code": 10107, "codeName": "NotMaster", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1595902580, "i": 1}}, "signature": {"hash": {"$binary": {"base64": "CtMA9l2pphTu5U2AmiPo1MgOmn0=", "subType": "00"}}, "keyId": 6803004021045985281}}}
        at com.mongodb.internal.connection.ProtocolHelper.createSpecialException(ProtocolHelper.java:244) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:171) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:302) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:258) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:500) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:71) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:224) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:202) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:118) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:110) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.operation.CommandOperationHelper.executeWriteCommand(CommandOperationHelper.java:369) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.operation.CommandOperationHelper.executeWriteCommand(CommandOperationHelper.java:360) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:284) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:277) ~[mongodb-driver-core-4.0.4.jar!/:?]
        at com.mongodb.internal.operation.CreateIndexesOperation$1.call(CreateIndexesOperation.java:171) ~[mongodb-driver-core-4.0.4.jar!/:?]

Hello.

  1. When I connect to a mongodb do I need to give the hostnames of all the mongo servers or just 1 is enough ?

It is a good practice to specify all host names of the replica-set in the connection string. An example, from Connection String URI Format:

mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl

But, you can also connect to a replica-set specifying a single hostname (but, this can be a problem if that particular host is down and you are trying to connect to the replica-set).

  1. This exception is coming for a write operation will it also come for read operation

The exception can occur for read operations also. com.mongodb.MongoNotPrimaryException definition says: “An exception indicating that the server is a member of a replica set but is not the primary, and therefore refused to execute either a write operation or a read operation that required a primary. This can happen during a replica set election.”

Regarding providing all the hostnames, I do see from the logs that even though I provide just one server name, it does discover all the servers in the replicaset. So will it connect to any server required from that server name provided ? Also will it connect to the primary only as my read and write operations are to be from primary only.

Regarding MongoNotPrimary exception.
So what can we do to avoid the exception ? Is there any setting which we can provide so it will wait for the election to happen ? Also I have an instance of Mongo client which is already connected to the DB , will it internally change the connection to connect to the new primary ?