Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoSocketReadException: Prematurely reached end of stream (after a period of inactivity)

I get this error on a find call (default Java Driver) after a period of inactivity. I tried to add a manual heartbeat (writing to a capped collection), but it didn't help. I only get the issue while being connected to an instance on compose (i.e. not in a local context).

MongoDB version is 3.2.8, latest driver (3.3), using Java 8.

Any idea ?

like image 869
Rhangaun Avatar asked Aug 22 '16 12:08

Rhangaun


4 Answers

I found it in some documentation:

For long running applications, it is often prudent to enable "keepAlive" with a number of milliseconds. Without it, after some period of time you may start to see "connection closed" errors for what seems like no reason.

Check if this helps. When you connect to mongoDB you can pass socket options to it. I am from node background we use following options to keep it alive.

server: {
        socketOptions: {
            keepAlive: 100,
            connectTimeoutMS: 30000
        }
    }

Hope this helps!!

like image 58
Sachin Avatar answered Oct 14 '22 20:10

Sachin


I solve this problem by set sslEnabled to true,code sample:

@Bean
public MongoClient mongoClient() {
    List<ServerAddress> saList = new ArrayList<>();
    saList.add(new ServerAddress("cluster0-shard-00-00-75shm.gcp.mongodb.net", 27017));
    saList.add(new ServerAddress("cluster0-shard-00-01-75shm.gcp.mongodb.net", 27017));
    saList.add(new ServerAddress("cluster0-shard-00-02-75shm.gcp.mongodb.net", 27017));

    char[] pwd =  "password".toCharArray();
    MongoCredential credential = MongoCredential.createCredential("username", "admin", pwd);

    //set sslEnabled to true here
    MongoClientOptions options = MongoClientOptions.builder()
            .readPreference(ReadPreference.primaryPreferred())
            .retryWrites(true)
            .requiredReplicaSetName("Cluster0-shard-0")
            .maxConnectionIdleTime(6000)
            .sslEnabled(true)
            .build();

    MongoClient mongoClient = new MongoClient(saList, credential, options);     
    return mongoClient;
}

Addition: my client jar is org.mongodb.mongodb-driver 3.6.4,server is mongodb atlas M0 3.6.6 on GCP

like image 42
iengchen Avatar answered Oct 14 '22 21:10

iengchen


This worked for me in spring boot and cloud based mongo (Atlas clustered instances).

Edit application.properties like this:

spring.data.mongodb.uri = mongodb+srv://username:[email protected]/dbname

For regular mongo instances (non-clustered) use this:

spring.data.mongodb.uri = mongodb://username:[email protected]:27017,hostname2:27017/dbname?ssl=true
  • If you want to set other connection options, you can chain multiple parameters using '&' ; documentation here: https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/MongoClientURI.html
  • If you were using other spring.data.mongodb parameters, you should remove all of them otherwise spring won't read spring.data.mongodb.uri
like image 6
jmojico Avatar answered Oct 14 '22 20:10

jmojico


I agree with Rhangaun's answer here is my solution in code of JAVA:

    public static DB getMongoDB() {

        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        //build the connection options  
        builder.maxConnectionIdleTime(60000);//set the max wait time in (ms)
        MongoClientOptions opts = builder.build();
        
    
        char[] password2 = "mypassword".toCharArray();
        
        MongoCredential credential2 = MongoCredential.createCredential("username", "databasename",password2);
        
       
        //add your option to the connection 

        MongoClient mongoClient = new MongoClient(new ServerAddress("server ip",27017), Arrays.asList(credential2),opts);
        //use your database 
        cachedDb = mongoClient.getDB("databasename");
        
    return cachedDb;

}

Here is my research link:http://3t.io/blog/how-to-prevent-your-connection-from-dropping-with-hosted-mongodb-instances/

Hope it helps you.

like image 5
Daqian Avatar answered Oct 14 '22 22:10

Daqian