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

前端 未结 6 1912
Happy的楠姐
Happy的楠姐 2020-12-05 02:34

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 d

6条回答
  •  一整个雨季
    2020-12-05 03:16

    The problem is that Mongodb end the connection. You need to increase the timeout of the Mongodb Driver here is an example code.

     MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        //build the connection options
        builder.maxConnectionIdleTime(86400000);//set the max wait time in (ms)
        MongoClientOptions opts = builder.build();
    
    
    
    
        final Morphia morphia = new Morphia();
    
    
        morphia.mapPackage("com.java.code");
    
    
        final String hostURL = "host_url";
                
    
        MongoCredential  credential = MongoCredential.createCredential("username","database","Password".toCharArray()); 
    
        ServerAddress address = new ServerAddress(hostURL);
    
    
        List credentialList = new ArrayList<>();
        credentialList.add(credential);
    
    
       final MongoClient client = new MongoClient(address,credentialList,opts);
    
    
    
    
        // create the Datastore connecting to the default port on the local host
        datastore  = morphia.createDatastore(client,"datastore");
    

提交回复
热议问题