【问题标题】:Elegant way to join Transactions in Reactive Vert.X在 Reactive Vert.X 中加入事务的优雅方式
【发布时间】:2022-01-14 15:24:20
【问题描述】:

假设我有一个服务方法,我在其中进行一些验证/休息调用等(例如 someServiceMethod2 in)并希望以事务方式使其安全。我还有一个包含交易的 repoMethod。 父事务抛出异常时如何回滚子事务?

有没有办法在事务中加入这两种方法?就像 TransactionDefinition.PROPAGATION_REQUIRED 传播在 Spring lib 中所做的一样。

fun someServiceMethod () {
    client.withTransaction { c ->
        val bla = someServiceMethod2() // works

        someRepo.doSthRepoStuff(bla)) // works

        throw Exception("Just for test purpose") // crashes -> should also rollback transaction from doSthRepoStuff
    }
}

...

fun doSthRepoStuff(bla : String) {
    client.withTransaction { c -> 
        // do db related stuff here
    }
}

我现在唯一能做的就是只使用服务事务并将连接传递给 repo 方法。这对我来说有点奇怪(给 repo 方法一个 sql 连接)

有没有优雅的方法来解决这个问题?

【问题讨论】:

    标签: java kotlin vert.x quarkus reactive-postgresql-client


    【解决方案1】:

    花了很多时间来解决这个问题,我能够实现这一点的唯一方法是传递以下类的对象

    io.vertx.reactivex.sqlclient.SqlConnection

    这很顺利。

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2011-01-24
      • 2016-12-11
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多