createTimeoutException: trying to connect using instance from another class


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)) {
			this.database = mongoClient.getDatabase("tg-db");
			this.collection = this.database.getCollection(this.collectionName);
			Document doc = Document.parse(record);

  • 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) {
  • 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");


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


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=[{, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by { extension (5) should not be presented in certificate_request}}, {, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by { extension (5) should not be presented in certificate_request}}, {, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by { extension (5) should not be presented in certificate_request}}]
	at com.mongodb.internal.connection.BaseCluster.createTimeoutException(
	at com.mongodb.internal.connection.BaseCluster.selectServer(
	at com.mongodb.internal.connection.AbstractMultiServerCluster.selectServer(
	at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(
	at com.mongodb.client.internal.MongoClientDelegate.createClientSession(
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(
	at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(
	at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(
	at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(
	at com.mongodb.client.internal.MongoCollectionImpl.insertOne(
	at com.mongodb.client.internal.MongoCollectionImpl.insertOne(
	at MongoDB.insert(
	at EventDao.insert(
	at EventDaoTest.main(

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,

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.