Search in sources :

Example 1 with MongoTransactionManager

use of org.springframework.data.mongodb.MongoTransactionManager in project spring-data-mongodb by spring-projects.

the class SimpleMongoRepositoryVersionedEntityTests method initTxTemplate.

TransactionTemplate initTxTemplate() {
    MongoTransactionManager txmgr = new MongoTransactionManager(template.getMongoDbFactory());
    TransactionTemplate tt = new TransactionTemplate(txmgr);
    tt.afterPropertiesSet();
    return tt;
}
Also used : MongoTransactionManager(org.springframework.data.mongodb.MongoTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate)

Example 2 with MongoTransactionManager

use of org.springframework.data.mongodb.MongoTransactionManager in project spring-data-mongodb by spring-projects.

the class SimpleMongoRepositoryTests method existsShouldBePossibleInTransaction.

// DATAMONGO-2130
@Test
@EnableIfReplicaSetAvailable
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
void existsShouldBePossibleInTransaction() {
    MongoTransactionManager txmgr = new MongoTransactionManager(template.getMongoDbFactory());
    TransactionTemplate tt = new TransactionTemplate(txmgr);
    tt.afterPropertiesSet();
    boolean exists = tt.execute(status -> {
        Person sample = new Person();
        sample.setLastname("Matthews");
        repository.save(sample);
        return repository.existsById(sample.getId());
    });
    assertThat(exists).isTrue();
}
Also used : MongoTransactionManager(org.springframework.data.mongodb.MongoTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Person(org.springframework.data.mongodb.repository.Person) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Example 3 with MongoTransactionManager

use of org.springframework.data.mongodb.MongoTransactionManager in project spring-data-mongodb by spring-projects.

the class SimpleMongoRepositoryTests method countShouldBePossibleInTransaction.

// DATAMONGO-2130
@Test
@EnableIfReplicaSetAvailable
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.0")
void countShouldBePossibleInTransaction() {
    MongoTransactionManager txmgr = new MongoTransactionManager(template.getMongoDbFactory());
    TransactionTemplate tt = new TransactionTemplate(txmgr);
    tt.afterPropertiesSet();
    long countPreTx = repository.count();
    long count = tt.execute(status -> {
        Person sample = new Person();
        sample.setLastname("Matthews");
        repository.save(sample);
        return repository.count();
    });
    assertThat(count).isEqualTo(countPreTx + 1);
}
Also used : MongoTransactionManager(org.springframework.data.mongodb.MongoTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Person(org.springframework.data.mongodb.repository.Person) EnableIfReplicaSetAvailable(org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable) EnableIfMongoServerVersion(org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion) Test(org.junit.jupiter.api.Test)

Aggregations

MongoTransactionManager (org.springframework.data.mongodb.MongoTransactionManager)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 Test (org.junit.jupiter.api.Test)2 Person (org.springframework.data.mongodb.repository.Person)2 EnableIfMongoServerVersion (org.springframework.data.mongodb.test.util.EnableIfMongoServerVersion)2 EnableIfReplicaSetAvailable (org.springframework.data.mongodb.test.util.EnableIfReplicaSetAvailable)2