use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.
the class SessionBoundMongoTemplateUnitTests method geoNearShouldUseProxiedDatabase.
// DATAMONGO-1880, DATAMONGO-2264
@Test
public void geoNearShouldUseProxiedDatabase() {
when(database.runCommand(any(ClientSession.class), any(), eq(Document.class))).thenReturn(new Document("results", Collections.emptyList()));
template.geoNear(NearQuery.near(new Point(0, 0), Metrics.NEUTRAL), Person.class);
verify(collection).aggregate(eq(clientSession), anyList(), eq(Document.class));
}
use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.
the class SessionBoundMongoTemplateUnitTests method groupShouldUseProxiedDatabase.
// DATAMONGO-1880
@Test
public void groupShouldUseProxiedDatabase() {
when(database.runCommand(any(ClientSession.class), any(), eq(Document.class))).thenReturn(new Document("retval", Collections.emptyList()));
template.group(COLLECTION_NAME, GroupBy.key("firstName"), Person.class);
verify(database).runCommand(eq(clientSession), any(), eq(Document.class));
}
use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class CausalConsistencyExamples method main.
/**
* Run this main method to see the output of this quick example.
*
* @param args takes an optional single argument for the connection string
*/
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
}
use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class TransactionExample method updateEmployeeInfo.
private void updateEmployeeInfo() {
MongoCollection<Document> employeesCollection = client.getDatabase("hr").getCollection("employees");
MongoCollection<Document> eventsCollection = client.getDatabase("reporting").getCollection("events");
TransactionOptions txnOptions = TransactionOptions.builder().readPreference(ReadPreference.primary()).readConcern(ReadConcern.MAJORITY).writeConcern(WriteConcern.MAJORITY).build();
try (ClientSession clientSession = client.startSession()) {
clientSession.startTransaction(txnOptions);
employeesCollection.updateOne(clientSession, Filters.eq("employee", 3), Updates.set("status", "Inactive"));
eventsCollection.insertOne(clientSession, new Document("employee", 3).append("status", new Document("new", "Inactive").append("old", "Active")));
commitWithRetry(clientSession);
}
}
use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class UnifiedTest method executeEndSession.
private OperationResult executeEndSession(final BsonDocument operation) {
ClientSession session = entities.getSession(operation.getString("object").getValue());
session.close();
return OperationResult.NONE;
}
Aggregations