diff --git a/persistence-modules/spring-data-mongodb/pom.xml b/persistence-modules/spring-data-mongodb/pom.xml
index eec1fa31dc..df16be1396 100644
--- a/persistence-modules/spring-data-mongodb/pom.xml
+++ b/persistence-modules/spring-data-mongodb/pom.xml
@@ -8,9 +8,9 @@
com.baeldung
- parent-boot-2
+ parent-boot-3
0.0.1-SNAPSHOT
- ../../parent-boot-2
+ ../../parent-boot-3
@@ -78,6 +78,7 @@
1.1.3
3.5.4
4.6.3
+ com.baeldung.Main
\ No newline at end of file
diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoReactiveConfig.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoReactiveConfig.java
index b4042b5550..6ec84fca1f 100644
--- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoReactiveConfig.java
+++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoReactiveConfig.java
@@ -1,11 +1,14 @@
package com.baeldung.config;
+import org.springframework.context.annotation.Bean;
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;
+import org.springframework.transaction.ReactiveTransactionManager;
+import org.springframework.transaction.reactive.TransactionalOperator;
@Configuration
@EnableReactiveMongoRepositories(basePackages = "com.baeldung.reactive.repository")
@@ -20,4 +23,9 @@ public class MongoReactiveConfig extends AbstractReactiveMongoConfiguration {
protected String getDatabaseName() {
return "reactive";
}
+
+ @Bean
+ public TransactionalOperator transactionalOperator(ReactiveTransactionManager reactiveTransactionManager) {
+ return TransactionalOperator.create(reactiveTransactionManager);
+ }
}
diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/transaction/MongoTransactionReactiveLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/transaction/MongoTransactionReactiveLiveTest.java
index 3fc8dcf977..209f5cbbee 100644
--- a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/transaction/MongoTransactionReactiveLiveTest.java
+++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/transaction/MongoTransactionReactiveLiveTest.java
@@ -6,11 +6,14 @@ 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.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.config.MongoReactiveConfig;
import com.baeldung.model.User;
+import org.springframework.transaction.reactive.TransactionalOperator;
+import reactor.core.publisher.Mono;
/**
*
@@ -25,6 +28,12 @@ public class MongoTransactionReactiveLiveTest {
@Autowired
private ReactiveMongoOperations reactiveOps;
+ @Autowired
+ private TransactionalOperator transactionalOperator;
+
+ @Autowired
+ private ReactiveMongoTemplate mongoTemplate;
+
@Before
public void testSetup() {
if (!reactiveOps.collectionExists(User.class)
@@ -45,9 +54,11 @@ public class MongoTransactionReactiveLiveTest {
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)));
+
+ Mono saveEntity1 = mongoTemplate.save(user1);
+ Mono saveEntity2 = mongoTemplate.save(user2);
+
+ saveEntity1.then(saveEntity2).then().as(transactionalOperator::transactional);
}
}