MongoDB timeout settings

MongoDB is composed of mongos, replica-set, and consists of four shards. (version 4.2.19)
Data is being loaded in real time on MongoDB.
In this situation, I would like to know the settings that recommend connectTimeoutMS, socketTimeoutMS, maxTimeMS, and KeepAlive.
I am changing the setting value and monitoring it, but the error below occurs frequently.

Exception sending message; nested exception is com.mongodb.MongoSocketWriteException: Exception sending message

Hi @rinjyu_N_A and welcome to the MongoDB community!!

It would be very helpful to understand the issue if you could help us with the following details :

  1. Are you observing any issue while using the default settings? Can you post the errors for the same?
  2. Could you help in understating the use case for modifying the values and what are the values at which the errors start to occur?

Also, there has been a community forum post related to a similar issue, would suggest if you could use the solution Exception sending message, MongoSocketWriteException.

P.S. 4.2.19 is an older MongoDB version. Please upgrade to 4.2.22 for bug fixes and new features introduced.


1 Like

Thank you for your reply.

I use spring-data-mongo 3.0.9 version and mongo driver 4.6 version.
There are two intermittent errors when saving data to mongodb in the spring boot application.

case 1 :
Exception sending message; nested exception is com.mongodb.MongoSocketWriteException

case 2 :
Prematurely reached end of stream; nested exception is com.mongodb.MongoSocketReadException

The application uses the save method in mongoTemplate when saving data.

The mongodb setting in the application.yml of the spring boot application is shown below.

            uri: mongodb://test:test@ip1:port1,ip2:port2,ip3:port3,ip4:port4/database?authSource=database&maxIdleTimeMS=360000&maxLifeTimeMS=360000

The error occurs regardless of whether maxIdleTimeMS and maxLifeTimeMS are set in the mongodb setting.

I tried to apply the MongoClient setting that you linked to the spring boot application, but the error was not resolved.

Is there any additional opinion that will help me?

Hi @rinjyu_N_A and thank you for sharing the above details

The above issue might have occurred due to message being transmitted over a closed socket.
Can you also confirm if there is pattern observed for the above problem. For example, if the app was running for a while?

The following post on stackoverflow might have a possible solution.



I will refer to your opinion.
Thanks for your help. :slightly_smiling_face: