createTimeoutException: trying to connect using instance from another class

Hello,

I recently started working with mongodb and java for a project. Although I managed to connect mongodb and apply all kind of operations (find, insert, delete, update) successfully, I encountered a very strange problem:

I use three classes: EventDao class, MongoDB class and EventDaoTest class. A brief description of the classes:

  • MongoDB class connects with mongoDB and inserts a doc. Example of code:
public void insert(String record) {
		try (MongoClient mongoClient = MongoClients.create(uri)) {
			System.out.println(record);
			this.database = mongoClient.getDatabase("tg-db");
			this.collection = this.database.getCollection(this.collectionName);
			
			Document doc = Document.parse(record);
			this.collection.insertOne(doc);
		}
	}

  • EventDao class uses the insert function from MongoDB class to insert an Event object into the database. Example of code:
public void insert(Event event) {
		this.mongoDB.insert(this.gson.toJson(event));
	}
  • EventDaoTest class contains a main for testing reasons (test the insert function). Example of code:
public static void main(String[] args) {
		EventDao eventDao = EventDao.getInstance();
		Event event = new Event("e101", "testing");
		eventDao.insert(event);
	}

Notes

As I have mentioned, I managed to connect to mongoDB by creating a main function in MongoDB class. The problem seems to be when I create from the EventDao class a MongoDB instance and Im trying to insert a record. I am always getting a createTimeoutException. Also, it is worth mentioning that I created a main function in MongoDB class, created an EventDao instance and it worked. So, I concluded that the main problem is when Im trying to invoke a function in MongoDB class from another class (here this is the EventDao class).

Does anyone know what goes so wrong? I have been struggling for hours.

Hi @Aggelos_Pipilikas
Welcome to the community forum!!

As I understand from the code snippet above, the insert method creates a MongoClient instance every time the method is invoked. Generally it would be recommended to create one MongoClient instance for all database operations within an application.

In addition, the createTimeoutException does not seem to originate from the MongoDB server. Could you provide the error stack trace related to the exception ?

Also could you provide the following information:

  • MongoDB server version
  • MongoDB server topology (standalone, replica set, sharded cluster)
  • MongoDB Java driver version

Thanks
Aasawari

Hi @Aasawari and thanks for the fast response!

First of all, yes you got it right. The insert method creates a MongoClient instace every time the mothod is invoked in a try statement so that after this operation has executed, the connection will be terminated.

I tried to create a MongoClient instance in the constructor method as you suggested (one instace for all operations) but I got the exact same error.

Here is the error stack trace that you asked for.

Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@34f7cfd9. Client view of cluster state is {type=REPLICA_SET, servers=[{address=cluster-shard-00-00.r0jin.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: extension (5) should not be presented in certificate_request}}, {address=cluster-shard-00-02.r0jin.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: extension (5) should not be presented in certificate_request}}, {address=cluster-shard-00-01.r0jin.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: extension (5) should not be presented in certificate_request}}]
	at com.mongodb.internal.connection.BaseCluster.createTimeoutException(BaseCluster.java:424)
	at com.mongodb.internal.connection.BaseCluster.selectServer(BaseCluster.java:121)
	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:207)
	at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1010)
	at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:471)
	at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:454)
	at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:448)
	at MongoDB.insert(MongoDB.java:78)
	at EventDao.insert(EventDao.java:54)
	at EventDaoTest.main(EventDaoTest.java:8)

Also I use 5.0.7 MongoDB server version, AWS / Frankfurt (eu-central-1), Replica Set - 3 nodes, M0-Sandbox and 4.5.1 MongoDB Java driver version. I hope this information helps you.

Thank you,
Aggelos

Hello everyone and thanks for dedicating some time in this topic.

I found what caused this problem. First of all, I use eclipse to build my gradle project. Second of all, the problem was in the TLS certificate request. I read that there are people with similar problem and, being one of them at first, I solved this one in my main function in MongoDB class. What I did was to pass the argument -Djdk.tls.client.protocols=TLSv1.2 at the “Run configurations” settings. If you want to call a MongoDB instance from another class, you have to pass the same argument at the “Run configurations” setting of the class in which you have implemented the main function. This solved the problem!

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