Skip to content

Commit

Permalink
[JAVA-31501] Upgrade spring-data-mongodb to Spring Boot 3 (eugenp#16037)
Browse files Browse the repository at this point in the history
  • Loading branch information
vunamtien authored Mar 5, 2024
1 parent f1105fb commit 39e9351
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 3 additions & 2 deletions persistence-modules/spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
<relativePath>../../parent-boot-3</relativePath>
</parent>

<dependencies>
Expand Down Expand Up @@ -78,6 +78,7 @@
<mysema.maven.version>1.1.3</mysema.maven.version>
<projectreactor.version>3.5.4</projectreactor.version>
<embed.mongo.version>4.6.3</embed.mongo.version>
<start-class>com.baeldung.Main</start-class>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -20,4 +23,9 @@ public MongoClient reactiveMongoClient() {
protected String getDatabaseName() {
return "reactive";
}

@Bean
public TransactionalOperator transactionalOperator(ReactiveTransactionManager reactiveTransactionManager) {
return TransactionalOperator.create(reactiveTransactionManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
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;

/**
*
Expand All @@ -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)
Expand All @@ -45,9 +54,11 @@ public void tearDown() {
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<User> saveEntity1 = mongoTemplate.save(user1);
Mono<User> saveEntity2 = mongoTemplate.save(user2);

saveEntity1.then(saveEntity2).then().as(transactionalOperator::transactional);
}

}

0 comments on commit 39e9351

Please sign in to comment.