Jukka Siivonen
11/28/2019, 3:16 PMtransactionTemplate.execute {
val bar = doSomethingTransactional()
if (bar) {
{ doSomethingOutsideTransaction(bar) }
} else {
{ doSomethingElseOutsideTransaction() }
}
}?.invoke()
So that part of the code is run inside transaction and some part of code is run outside transaction (with data found inside transaction). Example is simplified a lot from real use case so bear with me.. :)fun <T> TransactionTemplate.executeWithCallback(action: () -> () -> T): T? {
return this.execute { action.invoke() }?.invoke()
}
marstran
11/28/2019, 3:54 PMexecute
returns the result of the callback. Can't you just do this?
val bar = transactionTemplate.execute {
doSomethingTransactional()
}
if (bar) {
doSomethingOutsideTransaction(bar)
} else {
doSomethingElseOutsideTransaction()
}
Jukka Siivonen
11/28/2019, 7:26 PMtransactionTemplate.execute {
val bar = doSomethingTransactional()
if (bar) {
{ doSomethingOutsideTransaction(bar) }
} else {
val foo = ...
val baz = ...
{ doSomethingElseOutsideTransaction(foo, baz) }
}
}?.invoke()
marstran
11/29/2019, 10:51 AMsealed class BarResult
data class WithBar(bar: Bar) : BarResult
data class WithoutBar(foo: Foo, baz: Baz): BarResult
val res = transactionTemplate.execute {
val bar = doSomethingTransactional()
if (bar) WithBar(bar)
else WithoutBar(foo, baz)
}
when(res) {
is WithBar -> doSomethingOutsideTransaction(res.bar)
is WithoutBar -> doSomethingElseOutsideTransaction(res.foo, res.baz)
}
Jukka Siivonen
11/29/2019, 11:26 AM