dnowak
Satyam Agarwal
fun <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 }) }
A modern programming language that makes developers happier.