use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.
the class TransactionExample method updateEmployeeInfoUsingWithTransactionHelper.
@Test
public void updateEmployeeInfoUsingWithTransactionHelper() {
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.withTransaction(() -> {
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")));
return null;
}, txnOptions);
} catch (MongoException e) {
System.out.println("Transaction aborted. Caught exception during transaction.");
throw e;
}
}
use of com.mongodb.client.ClientSession in project morphia by mongodb.
the class MorphiaQuery method count.
@Override
public long count(CountOptions options) {
ClientSession session = datastore.findSession(options);
Document query = getQueryDocument();
return session == null ? getCollection().countDocuments(query, options) : getCollection().countDocuments(session, query, options);
}
use of com.mongodb.client.ClientSession in project morphia by mongodb.
the class LegacyQuery method findAndDelete.
@Override
public T findAndDelete(FindAndDeleteOptions options) {
MongoCollection<T> mongoCollection = options.prepare(getCollection());
ClientSession session = datastore.findSession(options);
return session == null ? mongoCollection.findOneAndDelete(getQueryDocument(), options) : mongoCollection.findOneAndDelete(session, getQueryDocument(), options);
}
use of com.mongodb.client.ClientSession in project morphia by mongodb.
the class LegacyQuery method delete.
@Override
public DeleteResult delete(DeleteOptions options) {
MongoCollection<T> collection = options.prepare(getCollection());
ClientSession session = datastore.findSession(options);
if (options.isMulti()) {
return session == null ? collection.deleteMany(getQueryDocument(), options) : collection.deleteMany(session, getQueryDocument(), options);
} else {
return session == null ? collection.deleteOne(getQueryDocument(), options) : collection.deleteOne(session, getQueryDocument(), options);
}
}
use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.
the class ClientSessionTests method shouldApplyClientSession.
// DATAMONGO-1880
@Test
void shouldApplyClientSession() {
ClientSession session = mongoClient.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
assertThat(session.getOperationTime()).isNull();
Document doc = template.withSession(() -> session).execute(action -> action.findOne(new Query(), Document.class, "test"));
assertThat(doc).isNotNull();
assertThat(session.getOperationTime()).isNotNull();
assertThat(session.getServerSession().isClosed()).isFalse();
session.close();
}
Aggregations