Search in sources :

Example 41 with ClientSession

use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.

the class ClientSessionTests method shouldReuseConfiguredInfrastructure.

// DATAMONGO-2241
@Test
void shouldReuseConfiguredInfrastructure() {
    ClientSession session = mongoClient.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
    MappingMongoConverter source = MappingMongoConverter.class.cast(template.getConverter());
    MappingMongoConverter sessionTemplateConverter = MappingMongoConverter.class.cast(template.withSession(() -> session).execute(MongoOperations::getConverter));
    assertThat(sessionTemplateConverter.getMappingContext()).isSameAs(source.getMappingContext());
    assertThat(ReflectionTestUtils.getField(sessionTemplateConverter, "conversions")).isSameAs(ReflectionTestUtils.getField(source, "conversions"));
    assertThat(ReflectionTestUtils.getField(sessionTemplateConverter, "instantiators")).isSameAs(ReflectionTestUtils.getField(source, "instantiators"));
}
Also used : ClientSession(com.mongodb.client.ClientSession) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Test(org.junit.jupiter.api.Test)

Example 42 with ClientSession

use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.

the class ClientSessionTests method withAbortedTransaction.

// DATAMONGO-1920
@Test
void withAbortedTransaction() {
    ClientSession session = mongoClient.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
    assertThat(session.getOperationTime()).isNull();
    session.startTransaction();
    SomeDoc saved = template.withSession(() -> session).execute(action -> {
        SomeDoc doc = new SomeDoc("id-2", "value2");
        action.insert(doc);
        return doc;
    });
    session.abortTransaction();
    session.close();
    assertThat(saved).isNotNull();
    assertThat(session.getOperationTime()).isNotNull();
    assertThat(template.exists(query(where("id").is(saved.getId())), SomeDoc.class)).isFalse();
}
Also used : ClientSession(com.mongodb.client.ClientSession) Test(org.junit.jupiter.api.Test)

Example 43 with ClientSession

use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.

the class ClientSessionTests method withCommittedTransaction.

// DATAMONGO-1920
@Test
void withCommittedTransaction() {
    ClientSession session = mongoClient.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
    assertThat(session.getOperationTime()).isNull();
    session.startTransaction();
    SomeDoc saved = template.withSession(() -> session).execute(action -> {
        SomeDoc doc = new SomeDoc("id-2", "value2");
        action.insert(doc);
        return doc;
    });
    session.commitTransaction();
    session.close();
    assertThat(saved).isNotNull();
    assertThat(session.getOperationTime()).isNotNull();
    assertThat(template.exists(query(where("id").is(saved.getId())), SomeDoc.class)).isTrue();
}
Also used : ClientSession(com.mongodb.client.ClientSession) Test(org.junit.jupiter.api.Test)

Example 44 with ClientSession

use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.

the class ClientSessionTests method shouldBeAbleToReadDbRefDuringTransaction.

// DATAMONGO-2490
@Test
void shouldBeAbleToReadDbRefDuringTransaction() {
    SomeDoc ref = new SomeDoc("ref-1", "da value");
    WithDbRef source = new WithDbRef("source-1", "da source", ref);
    ClientSession session = mongoClient.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
    assertThat(session.getOperationTime()).isNull();
    session.startTransaction();
    WithDbRef saved = template.withSession(() -> session).execute(action -> {
        template.save(ref);
        template.save(source);
        return template.findOne(query(where("id").is(source.id)), WithDbRef.class);
    });
    assertThat(saved.getSomeDocRef()).isEqualTo(ref);
    session.abortTransaction();
}
Also used : ClientSession(com.mongodb.client.ClientSession) Test(org.junit.jupiter.api.Test)

Example 45 with ClientSession

use of com.mongodb.client.ClientSession in project morphia by mongodb.

the class MorphiaQuery 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)

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