List of usage examples for com.mongodb ReadConcern MAJORITY
ReadConcern MAJORITY
To view the source code for com.mongodb ReadConcern MAJORITY.
Click Source Link
From source file:documentation.CausalConsistencyExamples.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes an optional single argument for the connection string *///from w w w . j a v a2 s .co m public static void main(final String[] args) { setupDatabase(); MongoClient client = MongoClients.create(); // Start Causal Consistency Example 1 // Example 1: Use a causally consistent session to ensure that the update occurs before the insert. ClientSession session1 = client .startSession(ClientSessionOptions.builder().causallyConsistent(true).build()); Date currentDate = new Date(); MongoCollection<Document> items = client.getDatabase("test").withReadConcern(ReadConcern.MAJORITY) .withWriteConcern(WriteConcern.MAJORITY.withWTimeout(1000, TimeUnit.MILLISECONDS)) .getCollection("test"); items.updateOne(session1, eq("sku", "111"), set("end", currentDate)); Document document = new Document("sku", "nuts-111").append("name", "Pecans").append("start", currentDate); items.insertOne(session1, document); // End Causal Consistency Example 1 // Start Causal Consistency Example 2 // Example 2: Advance the cluster time and the operation time to that of the other session to ensure that // this client is causally consistent with the other session and read after the two writes. ClientSession session2 = client .startSession(ClientSessionOptions.builder().causallyConsistent(true).build()); session2.advanceClusterTime(session1.getClusterTime()); session2.advanceOperationTime(session1.getOperationTime()); items = client.getDatabase("test").withReadPreference(ReadPreference.secondary()) .withReadConcern(ReadConcern.MAJORITY) .withWriteConcern(WriteConcern.MAJORITY.withWTimeout(1000, TimeUnit.MILLISECONDS)) .getCollection("items"); for (Document item : items.find(session2, eq("end", BsonNull.VALUE))) { System.out.println(item); } // End Causal Consistency Example 2 }
From source file:org.jooby.mongodb.MongoRx.java
License:Apache License
static MongoClientSettings.Builder settings(final ConnectionString cstr, final Config conf) { MongoClientSettings.Builder settings = MongoClientSettings.builder(); settings.clusterSettings(cluster(cstr, conf)); settings.connectionPoolSettings(pool(cstr, conf)); settings.heartbeatSocketSettings(socket("heartbeat", cstr, conf)); withStr("readConcern", conf, v -> settings.readConcern(Match(v.toUpperCase()) .option(Case("DEFAULT", ReadConcern.DEFAULT), Case("LOCAL", ReadConcern.LOCAL), Case("MAJORITY", ReadConcern.MAJORITY)) .getOrElseThrow(() -> new IllegalArgumentException("readConcern=" + v)))); withStr("readPreference", conf, v -> settings.readPreference(ReadPreference.valueOf(v))); settings.serverSettings(server(conf)); settings.socketSettings(socket("socket", cstr, conf)); settings.sslSettings(ssl(cstr, conf)); withStr("writeConcern", conf, v -> settings.writeConcern(Match(v.toUpperCase()) .option(Case("W1", WriteConcern.W1), Case("W2", WriteConcern.W2), Case("W3", WriteConcern.W3), Case("ACKNOWLEDGED", WriteConcern.ACKNOWLEDGED), Case("JOURNALED", WriteConcern.JOURNALED), Case("MAJORITY", WriteConcern.MAJORITY)) .getOrElseThrow(() -> new IllegalArgumentException("writeConcern=" + v)))); return settings; }