add mongodb transactions

This commit is contained in:
DOHA 2018-10-04 22:18:40 +03:00
parent e65d77a47a
commit 9c544345b3
5 changed files with 237 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package com.baeldung.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
@Configuration
@EnableReactiveMongoRepositories(basePackages = "com.baeldung.reactive.repository")
public class MongoReactiveConfig extends AbstractReactiveMongoConfiguration {
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
}
@Override
protected String getDatabaseName() {
return "reactive";
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.reactive.repository;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import com.baeldung.model.User;
public interface UserRepository extends ReactiveMongoRepository<User, String> {
}

View File

@ -0,0 +1,47 @@
package com.baeldung.transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.config.MongoReactiveConfig;
import com.baeldung.model.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoReactiveConfig.class)
public class MongoTransactionReactiveIntegrationTest {
@Autowired
private ReactiveMongoOperations reactiveOps;
@Before
public void testSetup() {
if (!reactiveOps.collectionExists(User.class)
.block()) {
reactiveOps.createCollection(User.class);
}
}
@After
public void tearDown() {
System.out.println(reactiveOps.findAll(User.class)
.count()
.block());
reactiveOps.dropCollection(User.class);
}
@Test
public void whenPerformTransaction_thenSuccess() {
User user1 = new User("Jane", 23);
User user2 = new User("John", 34);
reactiveOps.inTransaction()
.execute(action -> action.insert(user1)
.then(action.insert(user2)));
}
}

View File

@ -0,0 +1,68 @@
package com.baeldung.transaction;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.MongoTransactionManager;
import org.springframework.data.mongodb.SessionSynchronization;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import com.baeldung.config.MongoConfig;
import com.baeldung.model.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class MongoTransactionTemplateIntegrationTest {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private MongoTransactionManager mongoTransactionManager;
@Before
public void testSetup() {
if (!mongoTemplate.collectionExists(User.class)) {
mongoTemplate.createCollection(User.class);
}
}
@After
public void tearDown() {
mongoTemplate.dropCollection(User.class);
}
@Test
public void givenTransactionTemplate_whenPerformTransaction_thenSuccess() {
mongoTemplate.setSessionSynchronization(SessionSynchronization.ALWAYS);
TransactionTemplate transactionTemplate = new TransactionTemplate(mongoTransactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
mongoTemplate.insert(new User("Kim", 20));
mongoTemplate.insert(new User("Jack", 45));
};
});
Query query = new Query().addCriteria(Criteria.where("name")
.is("Jack"));
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(1));
}
}

View File

@ -0,0 +1,90 @@
package com.baeldung.transaction;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.MongoTransactionException;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.baeldung.config.MongoConfig;
import com.baeldung.model.User;
import com.baeldung.repository.UserRepository;
import com.mongodb.MongoCommandException;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MongoTransactionalIntegrationTest {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private UserRepository userRepository;
@Test
@Transactional
public void whenPerformMongoTransaction_thenSuccess() {
userRepository.save(new User("John", 30));
userRepository.save(new User("Ringo", 35));
Query query = new Query().addCriteria(Criteria.where("name")
.is("John"));
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(1));
}
@Test(expected = MongoTransactionException.class)
@Transactional
public void whenListCollectionDuringMongoTransaction_thenException() {
if (mongoTemplate.collectionExists(User.class)) {
mongoTemplate.save(new User("John", 30));
mongoTemplate.save(new User("Ringo", 35));
}
}
@Test(expected = MongoCommandException.class)
@Transactional
public void whenCountDuringMongoTransaction_thenException() {
userRepository.save(new User("John", 30));
userRepository.save(new User("Ringo", 35));
userRepository.count();
}
@Test
@Transactional
public void whenQueryDuringMongoTransaction_thenSuccess() {
userRepository.save(new User("Jane", 20));
userRepository.save(new User("Nick", 33));
List<User> users = mongoTemplate.find(new Query(), User.class);
assertTrue(users.size() > 1);
}
// ==== Using test instead of before and after due to @transactional doesn't allow list collection
@Test
public void setup() {
if (!mongoTemplate.collectionExists(User.class)) {
mongoTemplate.createCollection(User.class);
}
}
@Test
public void ztearDown() {
mongoTemplate.dropCollection(User.class);
}
}