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"));
}
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();
}
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();
}
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();
}
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);
}
}
Aggregations