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 ?