[JAVA-1808] Mongo java driver insertOne() NullPointerException Created: 11/May/15  Updated: 28/May/15  Resolved: 18/May/15

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 3.0.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: windofthesky Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

CentOS 6.5 64bits (Installed on vSphere virtual machine)


Issue Links:
Duplicate
duplicates JAVA-1811 NullPointerException if command respo... Closed
Backwards Compatibility: Major Change

 Description   

when use mongo java driver(3.0.0rc1) to insert doc into mongodb (3.0.0 ) which was installed on a virtual server built by VMware vSphere,the API insertOne() could not work. The call stack shown as below:
Caused by: java.lang.NullPointerException
at com.mongodb.connection.ProtocolHelper.isCommandOk(ProtocolHelper.java:61)
at com.mongodb.connection.WriteCommandProtocol.receiveMessage(WriteCommandProtocol.java:189)
at com.mongodb.connection.WriteCommandProtocol.execute(.java:76)
at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:66)
at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:37)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:155)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:219)
at com.mongodb.connection.DefaultServerConnection.insertCommand(DefaultServerConnection.java:108)
at com.mongodb.operation.MixedBulkWriteOperation$Run$2.executeWriteCommandProtocol(MixedBulkWriteOperation.java:416)
at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:604)
at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:363)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:148)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:141)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:186)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:177)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:141)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:72)
at com.mongodb.Mongo.execute(Mongo.java:745)
at com.mongodb.Mongo$2.execute(Mongo.java:728)
at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:482)
at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:277)

PS: this exception happens not all the time.and never happens on windows



 Comments   
Comment by Jeffrey Yemin [ 18/May/15 ]

Duplicates JAVA-1811

Comment by Jeffrey Yemin [ 13/May/15 ]

I don't think the socketTimeout default is the problem. It's just uncovering this bug, which is that sometimes the server will return a command response without an ok field, and the driver is not properly handling it. When this is fixed what's going to happen is that the driver will throw a MongoCommandException instead of a NullPointerException, as a command that responds without an ok field with some sort of truth value (true, or 1), was not a successfully executed command.

Comment by windofthesky [ 13/May/15 ]

Hi,Jeff.
I think the problem comes from these code as below:
static boolean isCommandOk(final BsonDocument response) {
if (!response.containsKey("ok"))

{ return false; }

BsonValue okValue = response.get("ok");
if (okValue.isBoolean())

{ return okValue.asBoolean().getValue(); }

else if (okValue.isNumber())

{ return okValue.asNumber().intValue() == 1; }

else

{ return false; }

}

and here is the things that may help:
> The document that the application is inserting
Document msgdoc = new Document();
msgdoc.append("msg_id", MSG_ID);
msgdoc.append("msg_content", msg);

> The list of indexes on the collection that the document is being inserted into.
I use the default _id index(cause this is a demo project).

>The topology of the server (standalone, replica set, sharded)
the mongo server is a standalone one.

>The server logs for the server that the insert was executed on.
here is the insert command log as below:
"2015-04-21T17:49:24.574+0800 I COMMAND [conn68] command t_MESSAGE_2015-4-21.$cmd command: insert { insert: "col_msg", ordered: true, documents: [

{ _id: ObjectId('55361d24b5d6e51b8001d728'), msg_id: "1920437", msg_content: "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ" }

] } keyUpdates:0 writeConflicts:0 numYields:0 reslen:40 locks:{} 136ms"

PS: according to the mongo java driver source code,in MongoClientOptions.java,the default "socketTimeout" is 0ms,which means no timeout. when I try to set the "socketTimeout" to 300ms,this NullPointerException will not happen anymore.So I think maybe we should modify the default "socketTimeout" to a proper value avoid the bad network condition.

Comment by Jeffrey Yemin [ 11/May/15 ]

The server should never return a command response without an ok field, and it appears that's what's happening. Can you provide any more information that could help us to reproduce this issue? Some things that may help:

  • The document that the application is inserting
  • The list of indexes on the collection that the document is being inserted into.
  • The topology of the server (standalone, replica set, sharded)
  • The server logs for the server that the insert was executed on.
Generated at Thu Feb 08 08:55:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.