Mongodb: `com.mongodb.MongoSocketReadException: Prematurely reached end of stream` with morphia

Gelin Luo picture Gelin Luo · Feb 10, 2017 · Viewed 18k times · Source

I've a simple data structure (the Transaction referenced below) to be inserted into mongodb:

{"amount":111,"debitAcc":"588188286231743e7d5c923d","type":"CHARGE"}

An I got the following error stack:

com.mongodb.MongoSocketReadException: Prematurely reached end of stream
        at com.mongodb.connection.SocketStream.read(SocketStream.java:88)
        at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494)
        at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224)
        at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:96)
        at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:440)
        at com.mongodb.connection.WriteCommandProtocol.receiveMessage(WriteCommandProtocol.java:262)
        at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:104)
        at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:67)
        at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:37)
        at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168)
        at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
        at com.mongodb.connection.DefaultServerConnection.insertCommand(DefaultServerConnection.java:118)
        at com.mongodb.operation.InsertOperation.executeCommandProtocol(InsertOperation.java:76)
        at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:139)
        at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:133)
        at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:422)
        at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:413)
        at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:133)
        at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:60)
        at com.mongodb.Mongo.execute(Mongo.java:845)
        at com.mongodb.Mongo$2.execute(Mongo.java:828)
        at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:342)
        at com.mongodb.DBCollection.insert(DBCollection.java:337)
        at com.mongodb.DBCollection.insert(DBCollection.java:328)
        at org.mongodb.morphia.DatastoreImpl.saveDocument(DatastoreImpl.java:1297)
        at org.mongodb.morphia.DatastoreImpl.tryVersionedUpdate(DatastoreImpl.java:1340)
        at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:1286)
        at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:775)
        at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:758)

My mongodb version is 3.4.2.

Intereting is that I don't have the issue on my local dev environment (mint linux 18.1). But it just can't work on my SIT environment, which is a ubuntu 16.04

Any idea?

Updates with code to insert the document

enter image description here

Where transactionDao.save(...) implementation could be found at:

https://github.com/actframework/act-morphia/blob/master/src/main/java/act/db/morphia/MorphiaDaoBase.java#L206

update 2

The system works with other writes (even with much bigger records)

Answer

It-Z picture It-Z · Feb 19, 2017

Most of the time this is a result of timeouts with long reads\writes.

try to increase the timeouts or remove them completely:

    MongoClientOptions.Builder options_builder = new MongoClientOptions.Builder();
    options_builder.maxConnectionIdleTime(<some_long_time>);
    MongoClientOptions options = options_builder.build();
    MongoClient mongo_db = new MongoClient ("your.db.address", options);