MongoDB supports transaction from version 4.0 onwards and from version 4.4 it also supports creating collections in transaction
I just enabled and tested transaction in mongoDB using free cluster provided by mongoDB atlas which uses mongoDB version 5.0
Following settings should work for mongoDB version 4.2+, although not tested yet.
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
</parent>
<properties>
<java.version>11</java.version>
<mongodb.version>4.4.0</mongodb.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.7.0</version>
</dependency>
MongoConfig.java
@Configuration
public class MongoConfig
{
@Bean
MongoTransactionManager transactionManager(MongoDatabaseFactory
mongoDatabaseFactory)
{
return new MongoTransactionManager(mongoDatabaseFactory);
}
}
then add @Transactional on function where you want to enable transaction.
It worked for me !