I try to save 2 Documents in the same transaction. But when an exception is thrown just after the first save(), the first document is still saved. as if there was no transaction at all.
I use MongoDB Atlas, version 6.0.11
here my service :
@Service
@RequiredArgsConstructor
public class ApiService {
private final FirstRepository firstRepository;
private final SecondRepository secondRepository;
@Transactional
public Mono<SecondDocument> save(FirstDocument firstDocument, SecondDocument secondDocument) {
firstRepository.save().subscribe();
if(true) {
throw new RuntimeException("test");
}
return secondRepository.save(secondDocument);
}
}
and my mongo's configuration class:
@Configuration
@EnableTransactionManagement
public class MongoReactiveConfig extends AbstractReactiveMongoConfiguration {
@Bean
ReactiveMongoTransactionManager transactionManager(ReactiveMongoDatabaseFactory factory) {
return new ReactiveMongoTransactionManager(factory);
}
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create("mongodb+srv://user:password@url/db&replicaSet=replicaSet");
}
@Override
protected String getDatabaseName() {
return "database";
}
}
and my spring boot app :
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
Can anyone help me understand why the firstDocument is still saved please ?