-
Type:
Bug
-
Resolution: Done
-
Priority:
Major - P3
-
Affects Version/s: 2.5
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
When an entity is inserted twice into a collection the returned WriteResult should throw a MongoException (DuplicateKey) from
writeResult.getLastError().throwOnError();
This is the case when the dbobject is inserted via collection.insert( dbo, WriteConcern.SAFE ), but not, when the dbobject is inserted via collection.insert( dbo ).
After writeResult.getLastError().throwOnError() the wrtieResult.err property is populated, but no exception has been thrown - which I had expected.
I tested this with the java driver from github master (commit e3c837a7 from 2011-03-04).
The following test case shows the described behaviour:
@Test
public void testDetectDuplicateKeyErrorWithDefaultWriteConcern() throws UnknownHostException, MongoException, InterruptedException {
final Mongo mongo = new Mongo( "localhost" );
final DBCollection collection = mongo.getDB( "foo" ).getCollection( "testcollection" );
final BasicDBObject dbo = new BasicDBObject();
collection.insert( dbo );
Thread.sleep( 2000 );
final WriteResult writeResult = collection.insert( dbo ); // when invoked with WriteConcern.SAFE the error is detected / exception thrown
try {
writeResult.getLastError().throwOnError();
// now the writeResult.err property is populated, but now exception is thrown
Assert.fail( "mongodb should complain about a duplicate key error index (MongoException.DuplicateKey), s.th. like the following:\n" +
"\"err\" : \"E11000 duplicate key error index: foo.testcollection.$id dup key: { : ObjectId('4d75fea23a75528813a7eedf') }\"" );
} catch( final Exception e )
}