10

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)

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139

3 Answers3

8

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);
It-Z
  • 1,961
  • 1
  • 23
  • 33
  • 1
    It is a write to save a very small object, I don't want to workaround this issue by tuning the connection timeout value. I need to understand why it takes long time if what you said is the direct cause. – Gelin Luo Feb 19 '17 at 20:52
  • Please add some more info: Any other writes are working? Why using transaction? Why you are setting the same transaction twice with New? – It-Z Feb 19 '17 at 21:10
  • Updated. `Transaction` is just a data model, not SQL Transaction. Good spot on two new Transaction. I should fix that. Actually the first new Transaction is used to print out the debug information, it was not there originally. But it doesn't impact the result – Gelin Luo Feb 19 '17 at 21:44
  • You run Query before the update? this might slow down the update. – It-Z Feb 19 '17 at 22:02
  • Have you tried maybe timing individual calls in `MODEL_TYPE.save`? maybe even stepping with the debugger and see if any call takes long time to compleat unexpectedly? that might point us to a more specific problem. – It-Z Feb 20 '17 at 18:30
3

I don't have the root cause, but in the end I get the issue fixed by changing a field type from BigDecimal to double.

The issue is found in our SIT environmet and one developer's windows environment which has a mongodb cluser setup.

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
2

The most likely reason is the compatibility version is set too low.

Try db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )

https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/

ogborstad
  • 2,309
  • 2
  • 19
  • 22
  • I confirm, that i had same exception with `BigDecimal` when CompatibilityVersion lower 3.4 – zella May 22 '17 at 07:55