[JAVA-2146] Don't get exceptions from driver when Write concern not met. Created: 08/Mar/16  Updated: 11/Sep/19  Resolved: 08/Mar/16

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

Type: Task Priority: Major - P3
Reporter: John Page Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Writing to a 3.2 Database - either a replica set or a sharded cluster.

Where the write concern cannot be satisfied - for example 1 + Arbiter all that is remaining in 2 + A cluster and a write concern of 2 I get no exceptions thrown when calling MongoCollection.bulkWrite().

If I have a hard error - such as no primary I get an exception - but anywhere I would expect that the BulkWriteException.WriteConcernError is populated - no exception is thrown.

Also - when submitting a batch and where I have continueOnError enabled, and unordered operations - I would expect an exception to be thrown but to be able to see the failure reason for each operations - similar to this [ https://docs.mongodb.org/manual/reference/method/BulkWriteResult/ ] , how do I get similar functionality through he Java driver?



 Comments   
Comment by John Page [ 08/Mar/16 ]

Thanks Jeff

Comment by Jeffrey Yemin [ 08/Mar/16 ]

About your second question: here's some sample code which shows how to see all the write errors in an unordered bulkWrite:

        MongoCollection<Document> collection = client.getDatabase("test").getCollection("java2146");
        collection.drop();
 
        List<InsertOneModel<Document>> requests = asList(new InsertOneModel<Document>(new Document("_id", 1)),
                new InsertOneModel<Document>(new Document("_id", 2)));
 
        collection.bulkWrite(requests);
 
        try {
            collection.bulkWrite(requests, new BulkWriteOptions().ordered(false));
        } catch (MongoBulkWriteException e) {
            for (BulkWriteError cur : e.getWriteErrors()) {
                System.out.println(cur.getIndex());
                System.out.println(cur.getCategory());
                System.out.println(cur.getCode());
                System.out.println(cur.getMessage());
                System.out.println();
            }
        }

It prints:

0
DUPLICATE_KEY
11000
E11000 duplicate key error collection: test.java2146 index: _id_ dup key: { : 1 }
 
1
DUPLICATE_KEY
11000
E11000 duplicate key error collection: test.java2146 index: _id_ dup key: { : 2 }

Comment by John Page [ 08/Mar/16 ]

Mistake in not setting wtimeout

Comment by John Page [ 08/Mar/16 ]

Sorry - This one is my fault - I didn't realise that you HAD to specify a wtimeout - I thought it had a default value. I'll close this works as designed.

Comment by John Page [ 08/Mar/16 ]

If I specify the write concern as above I do get an Exception thrown.

If however I specify it via the URI on connection (which is what I am doing) then the write concern is obeyed and
bwResult.getInsertedCount() returns 0 insertions when the write concern cannot be satisfied - however no exception is thrown.

Adding .withWriteConcern(WriteConcern.W2.withWTimeout(1, TimeUnit.SECONDS)) works but it's important that URI is the thing handling write concerns here.

Comment by Jeffrey Yemin [ 08/Mar/16 ]

Also note that as per the Javadoc the bulkWrite method throws a MongoBulkWriteException in this situation, not a BulkWriteException. BulkWriteException couldn't be re-used in the new CRUD API because of its dependence on the DBObject class.

Comment by Jeffrey Yemin [ 08/Mar/16 ]

I can't reproduce this. With this code:

        MongoCollection<BsonDocument> collection = client.getDatabase("test").getCollection("java2146", BsonDocument.class);
        collection
                .withWriteConcern(WriteConcern.W2.withWTimeout(1, TimeUnit.SECONDS))
                .bulkWrite(singletonList(new InsertOneModel<BsonDocument>(new BsonDocument())));

I get this exception:

Exception in thread "main" com.mongodb.MongoBulkWriteException: Bulk write operation error on server jeff.fios-router.home:27018. Write concern error: BulkWriteConcernError{code=64, message='waiting for replication timed out', details={ "wtimeout" : true }}. 

when I bring down one of the two data-bearing nodes in my replica set.

Do you see anything different about your test case from the one I'm using?

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