-
Type: Improvement
-
Resolution: Done
-
Priority: Minor - P4
-
Affects Version/s: 2.12.2
-
Component/s: Cluster Management, Connection Management
-
None
When restarting mongodb, the first request after mongodb has restarted fails.
Unable to find source-code formatter for language: title:testmongoreconnection.java. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
import com.mongodb.DB; import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; import org.junit.Before; import org.junit.Test; public class TestMongoReconnection { public static final int RESTART_DELAY = 20000; private MongoClient mongoClient; @Before public void setUp() throws Exception { MongoClientOptions mongoClientOptions = MongoClientOptions.builder() // .heartbeatFrequency(10) //check every 10ms .build(); mongoClient = new MongoClient("127.0.0.1", mongoClientOptions); } @Test public void first_request_after_mongo_restart_should_success() throws Exception { DB db = mongoClient.getDB("test"); db.getCollectionNames(); System.out.println(String.format("you have %sms to restart your mongodb server before the test resumes", RESTART_DELAY)); Thread.sleep(RESTART_DELAY); db.getCollectionNames(); } }
The DefaultServer$DefaultServerStateListener class should call connectionProvider.invalidate(); if the state changed from connected to connecting