Getting error when upgrading mongo java driver to 4.4

I am upgrading the mongo java driver from 3.11.1 to 4.4.1.

Seems like spring data mongo also needs to be upgraded, so I am going from 2.2.0.RELEASE to 3.0.0.RELEASE. If I didn’t upgrade the spring data mongo, then it is a problem because version 2.2.0 includes mongo java driver 3.X, which hinders the overall upgrade of the mongo java driver upgrade to 4.4.1.

My spring data commons is at 2.2.0.RELEASE.

Mongo client (that worked before):

++++++++++
<mongo:mongo-client id=“mongoClient” host="${mongo.host}" port="${mongo.port}" replica-set="${mongo.replica.set}"
credentials="${mongo.username}:#{mongoCredentials.password}@${mongo.dbname}">
<mongo:client-options connections-per-host="${mongo.connections.per.host}"
threads-allowed-to-block-for-connection-multiplier=“4”
connect-timeout="${mongo.connection.timeout}" max-wait-time="${mongo.max.wait.time}"
socket-keep-alive=“true” socket-timeout=“1500” ssl="${mongo.ssl}" />
</mongo:mongo-client>

++++++++++

When I deploy the app, I am getting this error. Never got it with the old configuration.

++++++++++

    Caused by: com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'there are no users authenticated' on server 127.0.0.1:27017
            at com.mongodb.internal.operation.FindOperation.lambda$execute$1(FindOperation.java:670)
            at com.mongodb.internal.operation.OperationHelper.lambda$withSourceAndConnection$2(OperationHelper.java:564)
            at com.mongodb.internal.operation.OperationHelper.withSuppliedResource(OperationHelper.java:589)
            at com.mongodb.internal.operation.OperationHelper.lambda$withSourceAndConnection$3(OperationHelper.java:563)
            at com.mongodb.internal.operation.OperationHelper.withSuppliedResource(OperationHelper.java:589)
            at com.mongodb.internal.operation.OperationHelper.withSourceAndConnection(OperationHelper.java:562)
            at com.mongodb.internal.operation.FindOperation.lambda$execute$2(FindOperation.java:661)
            at com.mongodb.internal.async.function.RetryingSyncSupplier.get(RetryingSyncSupplier.java:65)
            at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:692)
            at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:86)
            at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:191)
            at com.mongodb.client.internal.FindIterableImpl.first(FindIterableImpl.java:200)
            at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2906)
            at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2877)
            at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:2735)

++++++++++

If I make the following changes, eventually I get back to the same error as above:

Spring data mongo: 3.3.0 (instead of 2.2.0.RELEASE to 3.0.0.RELEASE, trying 3.3.0)
Spring data commons: 2.2.0.RELEASE to 2.5.0
Spring framework: From 5.2.1.RELEASE to 5.3.4

I updated my local mongo server to 4.4. I’m still getting the same error.

I updated my mongo client to this. No matter what i do, it times out.

	<mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}" replica-set="rs"
						credential="${mongo.username}:#{mongoCredentials.password}@${mongo.dbname}?uri.authMechanism=SCRAM-SHA-1">
		<mongo:client-settings  socket-connect-timeout="${mongo.connection.timeout}"
								connection-pool-max-size="${mongo.connections.per.host}"
								connection-pool-max-wait-time="${mongo.max.wait.time}"
								cluster-hosts="${mongo.replica.set}"/>
	</mongo:mongo-client>

Error:

    Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@1ae0fa2f. Client view of cluster state is {type=REPLICA_SET, servers=[]
            at com.mongodb.internal.connection.BaseCluster.createTimeoutException(BaseCluster.java:414)
            at com.mongodb.internal.connection.BaseCluster.selectServer(BaseCluster.java:122)
            at com.mongodb.internal.connection.AbstractMultiServerCluster.selectServer(AbstractMultiServerCluster.java:50)
            at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:146)
            at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:101)
            at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:291)
            at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:183)
            at com.mongodb.client.internal.FindIterableImpl.first(FindIterableImpl.java:200)
            at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2906)
            at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2877)
            at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:2735)

Hi @Arun_Lakhotia welcome to the community!

I’m not too sure what could have caused this, but it appears that the app failed to connect to the database.

Could you check the address of the MongoDB server, verify that the database is running by connecting to it via the mongo or mongosh shell?

Also if you revert all your version changes back to what it was before, does it start working again?

Best regards
Kevin

Thank you. Yes, I was able to connect to the DB by running the mongo shell.

If I reverted my changes, it worked again.

I was finally able to resolve this in this manner:

I converted the XML config to a Java app config.

From there, the error messages were more helpful. It didn’t like the connection string, it thought the part where I entered the user name and password was really the host.

I couldn’t figure out why, because the connection string looked right.

(When I was trying with the xml config, I tried both using the credential string above, as well as a connection string like this:

< connection-string=“mongodb://${mongo.username}:#{mongoCredentials.password}@${mongo.host}/${mongo.dbname}?replicaSet=rs0&authSource=admin&uri.authMechanism=SCRAM-SHA-1”>

I tried many variations, hardcoding, or putting the replica set where mongo.host was. Every time, I’d get that server timeout error.)

I then created the same connection string via the Java app config (instead of using the xml based config).

After getting the message via the Java app config that it couldn’t find the host (and it then printed out the user:password as the host), I then moved the credentials to the mongo client settings object, and created them via MongoCredential.createCredential().

That way, my connection string just had the replica set, database name, and ssl enabled parameter.

Then, I was able to connect when my app deployed. No more timeout error.

When I was getting the server timeouts via the xml config, it didn’t tell me what host it was trying to connect to, like when I had the java app config.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.