[JAVA-31501] Upgrade spring-data-mongodb to Spring Boot 3 (#16037)

This commit is contained in:
vunamtien 2024-03-05 20:56:24 +07:00 committed by GitHub
parent f1105fb6fe
commit 39e93518ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 5 deletions

View File

@ -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>
@ -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>

View File

@ -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);
}
}

View File

@ -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<User> saveEntity1 = mongoTemplate.save(user1);
Mono<User> saveEntity2 = mongoTemplate.save(user2);
saveEntity1.then(saveEntity2).then().as(transactionalOperator::transactional);
}
}