I have a Spring Boot application with Mongo DB, which is deployed on Azure.
Now if I do not call the API for a long time, maybe some hours and call it my first call always fails with the exception attached. But then it starts to work fine. It seems that the database goes into an idle mode and then whenever the query is made for the first time it is still sleeping hence the error. Is this understanding correct? How to fix it?
Yes, your understanding is correct i.e, the database goes into an idle mode and whenever the query is made for the first time it is still sleeping hence the error occurring.
The above exception is due to the connection timeout.
It closes the connection because of passing the maximum connection idle time.
You will need to set this property according to your requirement.
maxConnectionIdleTime
This can be set either on your Mongo configuration or application profile.