Search in sources :

Example 36 with ClientSession

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();
}
Also used : ClientSession(com.mongodb.client.ClientSession) GeospatialIndex(org.springframework.data.mongodb.core.index.GeospatialIndex) Point(org.springframework.data.geo.Point) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Test(org.junit.jupiter.api.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 37 with ClientSession

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));
}
Also used : ClientSession(com.mongodb.client.ClientSession) CountDownLatch(java.util.concurrent.CountDownLatch) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Point(org.springframework.data.geo.Point) DataAccessException(org.springframework.dao.DataAccessException) LazyLoadingException(org.springframework.data.mongodb.LazyLoadingException) ClientSessionException(org.springframework.data.mongodb.ClientSessionException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 38 with ClientSession

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();
}
Also used : ClientSession(com.mongodb.client.ClientSession) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Test(org.junit.jupiter.api.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 39 with ClientSession

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

Example 40 with ClientSession

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