[JAVA-2135] MongoClient object accepts setWriteConcern which seems to have no effect Created: 02/Mar/16  Updated: 18/Mar/16  Resolved: 18/Mar/16

Status: Closed
Project: Java Driver
Component/s: API, Write Operations
Affects Version/s: 3.0.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Asya Kamsky Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to JAVA-2137 Deprecate Mongo property mutators Closed

 Description   

The following code snippet does not set WriteConcern on the MongoDatabase instance as might be expected:

MongoClient mongoClient = new MongoClient( <something> );
mongoClient.setWriteConcern(WriteConcern.REPLICAS_SAFE);
MongoDatabase db = mongoClient.getDatabase("wc");



 Comments   
Comment by Jeffrey Yemin [ 03/Mar/16 ]

See related ticket JAVA-2137.

Comment by Jeffrey Yemin [ 03/Mar/16 ]

I see the issue. The MongoClient.getDatabase method constructs a MongoDatabase instance with the write concern (and read preference) from MongoClientOptions instance rather than its own write concern (and read preference) property, which is initialized from MongoClientOptions but then can be changed via Mongo#setWriteConcern.

Mongo#getDB works as expected.

In practice, it's much more common for users to override WriteConcern default via MongoClientOptions or MongoClientURI, but as setWriteConcern is still supported, this should still be considered a bug.

Comment by Asya Kamsky [ 02/Mar/16 ]

Workarounds or scenarios that do work correctly:

  1. using MongoClientOptions class - something like
    MongoClientOptions clientOptions = MongoClientOptions.builder().writeConcern(WriteConcern.MAJORITY).build(); followed by passing clientOptions to MongoClient constructor.
  2. setting writeConcern on database or collection
  3. using the old DB class with client.getDB("wc") does inherit the writeConcern as expected.

The only combination that doesn't work is the old way of setting writeConcern on the client and the new getDatabase method.

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