use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.
the class SessionBoundMongoTemplateTests method countWithGeoInTransaction.
// DATAMONGO-2012
@Test
@MongoVersion(asOf = "4.0")
public void countWithGeoInTransaction() {
if (!template.collectionExists(Person.class)) {
template.createCollection(Person.class);
template.indexOps(Person.class).ensureIndex(new GeospatialIndex("location"));
} else {
template.remove(Person.class).all();
}
ClientSession session = client.startSession();
session.startTransaction();
MongoTemplate sessionBound = template.withSession(session);
sessionBound.save(new Person("Kylar Stern"));
assertThat(sessionBound.query(Person.class).matching(query(where("location").near(new Point(1, 0)))).count()).isZero();
session.commitTransaction();
session.close();
}
use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.
the class SessionBoundMongoTemplateTests method countShouldReturnIsolatedCount.
// DATAMONGO-2001
@Test
@MongoVersion(asOf = "4.0")
public void countShouldReturnIsolatedCount() throws InterruptedException {
if (!template.collectionExists(Person.class)) {
template.createCollection(Person.class);
} else {
template.remove(Person.class).all();
}
int nrThreads = 2;
CountDownLatch savedInTransaction = new CountDownLatch(nrThreads);
CountDownLatch beforeCommit = new CountDownLatch(nrThreads);
List<Object> resultList = new CopyOnWriteArrayList<>();
Runnable runnable = () -> {
ClientSession session = client.startSession();
session.startTransaction();
try {
MongoTemplate sessionBound = template.withSession(session);
try {
sessionBound.save(new Person("Kylar Stern"));
} finally {
savedInTransaction.countDown();
}
savedInTransaction.await(1, TimeUnit.SECONDS);
try {
resultList.add(sessionBound.query(Person.class).count());
} finally {
beforeCommit.countDown();
}
beforeCommit.await(1, TimeUnit.SECONDS);
} catch (Exception e) {
resultList.add(e);
}
session.commitTransaction();
session.close();
};
List<Thread> threads = //
IntStream.range(0, nrThreads).mapToObj(//
i -> new Thread(runnable)).peek(//
Thread::start).collect(Collectors.toList());
for (Thread thread : threads) {
thread.join();
}
assertThat(template.query(Person.class).count()).isEqualTo(2L);
assertThat(resultList).hasSize(nrThreads).allMatch(it -> it.equals(1L));
}
use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.
the class SessionBoundMongoTemplateTests method countShouldWorkInTransactions.
// DATAMONGO-2001
@Test
@MongoVersion(asOf = "4.0")
public void countShouldWorkInTransactions() {
if (!template.collectionExists(Person.class)) {
template.createCollection(Person.class);
} else {
template.remove(Person.class).all();
}
ClientSession session = client.startSession();
session.startTransaction();
MongoTemplate sessionBound = template.withSession(session);
sessionBound.save(new Person("Kylar Stern"));
assertThat(sessionBound.query(Person.class).matching(query(where("firstName").is("foobar"))).count()).isZero();
assertThat(sessionBound.query(Person.class).matching(query(where("firstName").is("Kylar Stern"))).count()).isOne();
assertThat(sessionBound.query(Person.class).count()).isOne();
session.commitTransaction();
session.close();
}
use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.
the class SessionAwareMethodInterceptorUnitTests method proxyFactoryOnDatabaseWithSessionInArgumentListProceedsWithExecution.
// DATAMONGO-1880
@Test
public void proxyFactoryOnDatabaseWithSessionInArgumentListProceedsWithExecution() {
ClientSession yetAnotherSession = mock(ClientSession.class);
database.drop(yetAnotherSession);
verify(targetDatabase).drop(eq(yetAnotherSession));
}
use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.
the class SessionAwareMethodInterceptorUnitTests method proxyFactoryOnCollectionWithSessionInArgumentListProceedsWithExecution.
// DATAMONGO-1880
@Test
public void proxyFactoryOnCollectionWithSessionInArgumentListProceedsWithExecution() {
ClientSession yetAnotherSession = mock(ClientSession.class);
collection.find(yetAnotherSession);
verify(targetCollection).find(eq(yetAnotherSession));
}
Aggregations