Java Driver - DuplicateKeyException no longer used?

312 views
Skip to first unread message

Guillaume Forget

unread,
Sep 15, 2015, 3:32:40 PM9/15/15
to mongodb-user
Hello,

I'm switching to use the new Document API in the Java driver. And it seems the DuplicateKeyException is no longer thrown. We're now getting com.mongodb.MongoWriteException. Previous code used to check the error key and would throw this specific type of exception. Is this expected? If yes, I would recommend calling it on the "upgrading to 3.0" page because this is significant change that affects client code. 

E11000 duplicate key error collection: shopstyle.properties index: n_1 dup key: { : "LastProfileId" }
  code
: 11000
  error
: WriteError{code=11000, message='E11000 duplicate key error collection: shopstyle.properties index: n_1 dup key: { : "LastProfileId" }', details={ }}
  serverAddress
: 127.0.0.1:27017
com
.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:487)
com
.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:277)

Thanks

Gui Forget

Jeff Yemin

unread,
Sep 15, 2015, 4:32:16 PM9/15/15
to mongodb-user
Hi Gui,

Yes, this is expected behavior, and I'm sorry to hear that it took you by surprise.  While the expected exceptions are included in the Javadoc for the various write methods in the new CRUD API, I can certainly see why it would cause confusion given that the exceptions are unchecked.

I added a Jira ticket to track your request to augment the Upgrading to 3.0 page.  You can track it here.


Regards,
Jeff
Reply all
Reply to author
Forward
0 new messages