[JAVA-93] DBCollection.save() doesn't throw duplicate key exception Created: 17/Mar/10  Updated: 17/Mar/10  Resolved: 17/Mar/10

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

Type: Bug Priority: Major - P3
Reporter: Vladimir Lifar Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Major Change

 Description   

Steps to reproduce:
1) create unique index:
test.ensureIndex(new BasicDBObject("field", 1), "fieldIndex", true);
2) save object

{field : 1, d : 1}

3) save object

{field : 1, d : 2}

Expected: exception on step 3
Detected: no exception. first object stored in DB.

Unit test:
@Test
public void testUniqueViolation() throws UnknownHostException {
Mongo mongo = new Mongo();
mongo.dropDatabase("testdb");
DB db = mongo.getDB("testdb");
DBCollection test = db.getCollection("test");
test.ensureIndex(new BasicDBObject("field", 1), "fieldIndex", true);

test.save(new BasicDBObject("field", 1).append("discriminator", 1));
try

{ test.save(new BasicDBObject("field", 1).append("discriminator", 2)); Assert.fail(); }

catch (MongoException e) {
}
}



 Comments   
Comment by Vladimir Lifar [ 17/Mar/10 ]

Eilot,
thanks for reply. I'd like to add some notes about these modes.
I've found writeConcern property by a chance. It's not documented in Wiki and in JavaDocs. Some words about this property may help to other users of Mongo.
Also it looks strange for me that default mode is 'NORMAL'. I suppose that DBMS is a kind of environment where errors are critical to user.

Comment by Eliot Horowitz (Inactive) [ 17/Mar/10 ]

if you want insert exception you have to turn on STRICT mode

Generated at Thu Feb 08 08:51:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.