Exception sending message, MongoSocketWriteException

Hi everyone,
I have a springboot project which comes with mongodb-driver-sync:4.2.3. In this application i am listenning some rabbitmq queue and saving to objects sent to this queue to mongodb. Mongodb database version is 4.4.11-debian-10-r12 from bitnami.

The persisting objects are just fine up to 1000 sequential inserts. But sometimes there are almost 2000 objects in the queue and when saving those objects one by one i got the error below. I think driver is combining single inserts into a batch of bulk insert, thefore i havent thought about changing my code and using the bulk insert.

I am wondering if this could be a networking issue, misusing the driver or a bug in the driver.

Thanks

Caused by: com.mongodb.MongoSocketWriteException: Exception sending message
at com.mongodb.internal.connection.InternalStreamConnection.translateWriteException(InternalStreamConnection.java:618)
at com.mongodb.internal.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:496)
at com.mongodb.internal.connection.InternalStreamConnection.sendCommandMessage(InternalStreamConnection.java:327)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:277)
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:100)
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:490)
at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:71)
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:253)
at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:202)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:118)
at com.mongodb.internal.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:431)
at com.mongodb.internal.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:251)
at com.mongodb.internal.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:76)
at com.mongodb.internal.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:194)
at com.mongodb.internal.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:185)
at com.mongodb.internal.operation.OperationHelper.withReleasableConnection(OperationHelper.java:621)
at com.mongodb.internal.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:185)
at com.mongodb.internal.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:76)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:187)
at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1009)
at com.mongodb.client.internal.MongoCollectionImpl.executeReplaceOne(MongoCollectionImpl.java:567)
at com.mongodb.client.internal.MongoCollectionImpl.replaceOne(MongoCollectionImpl.java:550)
at org.springframework.data.mongodb.core.MongoTemplate.lambda$saveDocument$18(MongoTemplate.java:1539)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:553)
... 55 common frames omitted
Caused by: java.net.SocketException: Broken pipe (Write failed)
at java.base/java.net.SocketOutputStream.socketWrite0(Native Method)
at java.base/java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.base/java.net.SocketOutputStream.write(Unknown Source)
at com.mongodb.internal.connection.SocketStream.write(SocketStream.java:99)
at com.mongodb.internal.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:493)
... 77 common frames omitted","thread_name":"org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-38669"}

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);
}
1 Like