springboot2.3.x uses mongodb transaction

mongodb must be a replica set or shard, not much nonsense, the code:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.MongoTransactionManager;


/**
 * 如果使用事务 mongodb必须使用副本集或分片
 */
@Configuration
public class MongoDBTransactionConfig {
    @Bean
    public MongoTransactionManager transactionManager(MongoDatabaseFactory factory){
        return new MongoTransactionManager(factory);
    }
}

In this way, the configuration is complete, just add transaction annotations where you need to use @Transactional(rollbackFor = Exception.class)

Guess you like

Origin blog.csdn.net/qq_42407917/article/details/111611446