美文网首页
Reactive Transaction On Spring D

Reactive Transaction On Spring D

作者: RJ_Hwang | 来源:发表于2019-06-19 21:52 被阅读0次

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

    Maven 配置:

    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongodb-driver-reactivestreams</artifactId>
      <version>1.9.2</version>
    </dependency>
    <dependency>
      <groupId>io.projectreactor</groupId>
      <artifactId>reactor-test</artifactId>
      <version>3.2.0.RELEASE</version>
      <scope>test</scope>
    </dependency>
    

    使用 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 D

          本文链接:https://www.haomeiwen.com/subject/epilqctx.html