dnowak
04/08/2020, 8:58 AMSatyam Agarwal
04/08/2020, 11:55 AMfun <A> DataSource.useTxn(f: (Handle) -> IOOf<A>): IO<A> {
return IO { this.open().begin() }
.bracketCase(release = { h, e -> h.closeTransaction(e) }, use = f)
}
fun Handle.closeTransaction(exitCase: ExitCase<Throwable>): IO<Unit> {
return IO {
when (exitCase) {
is ExitCase.Cancelled -> this.rollback().close()
is ExitCase.Completed -> this.commit().close()
is ExitCase.Error -> this.rollback().close()
}
}.guarantee(IO { this.close() })
}
DataSource.useTxn { h ->
IO { query1 }
.followedBy(IO { query2 })
.followedBy(IO { query3 })
.followedBy(IO { query4 })
.followedBy(IO { query5 })
}
dnowak
04/08/2020, 12:57 PMSatyam Agarwal
04/08/2020, 1:45 PMdnowak
04/08/2020, 1:57 PM