Search in sources :

Example 11 with ClientSession

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;
    }
}
Also used : MongoException(com.mongodb.MongoException) TransactionOptions(com.mongodb.TransactionOptions) ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) Test(org.junit.Test) ClusterFixture.isDataLakeTest(com.mongodb.ClusterFixture.isDataLakeTest)

Example 12 with ClientSession

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);
}
Also used : ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document)

Example 13 with ClientSession

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);
}
Also used : ClientSession(com.mongodb.client.ClientSession)

Example 14 with ClientSession

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);
    }
}
Also used : ClientSession(com.mongodb.client.ClientSession)

Example 15 with ClientSession

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();
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Aggregations

ClientSession (com.mongodb.client.ClientSession)52 BsonDocument (org.bson.BsonDocument)19 BsonValue (org.bson.BsonValue)17 BsonString (org.bson.BsonString)16 Map (java.util.Map)15 Document (org.bson.Document)12 Test (org.junit.jupiter.api.Test)10 BsonArray (org.bson.BsonArray)6 SessionBoundMongoTemplate (org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate)4 TransactionOptions (com.mongodb.TransactionOptions)3 MongoClient (com.mongodb.client.MongoClient)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)3 Test (org.junit.Test)3 Point (org.springframework.data.geo.Point)3 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)3 DeleteOptions (com.mongodb.client.model.DeleteOptions)2 FindOneAndDeleteOptions (com.mongodb.client.model.FindOneAndDeleteOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 NonNull (com.mongodb.lang.NonNull)2