Elegant way to join Transactions in Reactive Vert.X

Viewed 135

Let's say I have a service method where I do some validation/restcalls etc. (e.g. someServiceMethod2 in) and want to make it safe in a transactional way. I also have a repoMethod which includes a transaction. How can I rollback the child transaction when the parent transaction throws an exception ?

Is there a way to join these two methods in a transaction? Just like what the TransactionDefinition.PROPAGATION_REQUIRED propagation would do in 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
    }
}

The only way I could do it right now is to use only the service transaction and pass the connection to the repo method. This somehow feels weird to me (to give a repo method a sql connection)

Is there an elegant way to solve this ?

1 Answers

Spent a lot of time to figure this out and the only way i was able to achieve this is by passing around the object of following class

io.vertx.reactivex.sqlclient.SqlConnection

And this worked pretty smooth.

Related