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

前端 未结 6 1915
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:24

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

    @Bean
    public MongoClient mongoClient() {
        List 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

提交回复
热议问题