Reactive Transaction On Spring Data MongoDB

MongoDB 从 4.0 版开始支持多文档 ACID 事务,Spring Data 则从 Lovelace 版开始支持原生 MongoDB 事务。

Maven 配置:


  org.mongodb
  mongodb-driver-reactivestreams
  1.9.2


  io.projectreactor
  reactor-test
  3.2.0.RELEASE
  test

使用 ReactiveMongoOperations.inTransaction() :

@Autowired
private ReactiveMongoOperations reactiveOps;
 
@Test
public void test() {
  User user1 = new User("Jane", 23);
  User user2 = new User("John", 34);
  reactiveOps.inTransaction()
    .execute(action -> action.insert(user1)
    .then(action.insert(user2)));
}

From Spring Data MongoDB Transactions.
Last modified: November 23, 2018

你可能感兴趣的:(Reactive Transaction On Spring Data MongoDB)