Hi, I’m looking for some examples on how to use JDBC with Arrow. I want to run a bunch of IO effects...
d
Hi, I’m looking for some examples on how to use JDBC with Arrow. I want to run a bunch of IO effects in a single DB transaction. Is it doable?
s
yes. Try :
Copy code
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 })
}
😍 1
d
Is it “template” or is it based on some existing JDBC library?
s
A template 🙂
d
🙂 OK - I get the idea