Howto Catch Duplicate Key Exception in Java

1,615 views
Skip to first unread message

PHeinze

unread,
Feb 3, 2012, 3:14:03 PM2/3/12
to mongodb-user
Hi,

when I try to insert a duplicate through this code:

<em>
try {

coll.findAndModify( queryId( document ).and( "p.revision" ).is( document.getRevision() ).get(),
new BasicDBObject( "$set", mods ) );
}
}
catch ( MongoException e )
{
</em>

it correctly throws an Exception. But I can't retrieve the correct
error code through .getCode() (it's always -5). Sadly I can't cast to
CommandFailure the Subclass which is thrown as this is package
private. Do I miss something or is the class wrongly private?
The Exception I catch is following:
<em>
com.mongodb.CommandResult$CommandFailure: command failed [command
failed [findandmodify] { "serverUsed" : "pheinze-0830:27017" ,
"lastErrorObject" : { "err" : "E11000 duplicate key error index:
test.address.$ns_1_t_1_p.doc_id_1 dup key: { : \"__nn__\", : \"N\", :
\"testPutPKTwice\" }" , "code" : 11000 , "n" : 0 , "connectionId" :
44 , "ok" : 1.0} , "errmsg" : "E11000 duplicate key error index:
test.address.$ns_1_t_1_p.doc_id_1 dup key: { : \"__nn__\", : \"N\", :
\"testPutPKTwice\" }" , "ok" : 0.0}
</em>

I know that I could call getlasterror and retrieve from there the
correct error code, but I don't feel comfortable with this solution as
the correct exception is already present but not accessable.

Thanks for your help

Barrie

unread,
Feb 6, 2012, 11:51:55 AM2/6/12
to mongodb-user
Can you please file this as a Jira issue in the Java project?

Eliot Horowitz

unread,
Feb 6, 2012, 12:10:08 PM2/6/12
to mongod...@googlegroups.com
You can catch MongoException.DuplicateKey

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>

PHeinze

unread,
Feb 7, 2012, 5:13:39 AM2/7/12
to mongod...@googlegroups.com
I thought that too but MongoException.DuplicateKey isn't thrown. The only usage of DuplicateKey I could find is this line within CommandResult.java:
<em>
if ( code == 11000 || code == 11001 || s.startsWith( "E11000" ) || s.startsWith( "E11001" ) )
                    return new MongoException.DuplicateKey( code , s );
</em>
Within CommandResult.getException.

But the exception which is raised is a CommandResult.CommandFailure Exception which is no subclass of CommandResult but MongoException, so I can't call getException. If I miss something I'm happy if you can point it out, else I think this is a bug and I'll file a new ticket.
You can catch MongoException.DuplicateKey

> To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.

PHeinze

unread,
Feb 7, 2012, 2:42:15 PM2/7/12
to mongod...@googlegroups.com
I've just opened a ticket on jira for it: https://jira.mongodb.org/browse/JAVA-517
Reply all
Reply to author
Forward
0 new messages