Exception sending message, MongoSocketWriteException

For the ones who are suffering from this issue, the solution was to configure connection pool in my application. i have used below configuration and it has fixed the issue in my case. Initially java driver does not set maxlifetime for connection and open connections lives forever. I have also configured maxidletime for my connections so i can assure i have a valid fresh connection any time.

@Override
public MongoClient mongoClient() {
    ConnectionString connectionString = new ConnectionString(String.format(CONNECTION_STRING, host, port, database));
    MongoClientSettings clientSettings = MongoClientSettings.builder()
            .retryWrites(true)
            .applyConnectionString(connectionString)
            .applyToConnectionPoolSettings((ConnectionPoolSettings.Builder builder) -> {
                builder.maxSize(100) //connections count
                        .minSize(5)
                        .maxConnectionLifeTime(30, TimeUnit.MINUTES)
                        .maxConnectionIdleTime( maxIdleTime, TimeUnit.MILLISECONDS);
            })
            .applyToSocketSettings(builder -> {
                builder.connectTimeout(2000, TimeUnit.MILLISECONDS);
            })
            .applicationName(appName)
            .build();

    return MongoClients.create(clientSettings);
}
2 Likes