I'm confused by the way the slick 3 documentation describes transactions. I have slick 2 code that looks like this:
def doSomething(???) = DB.withTransaction { implicit session =>
userDao.doSomething(???)
addressDao.doSomething(???)
contactDao.doSomething(???)
}
How can i span a transaction in slick 3?
Please have a look at the documentation here http://slick.typesafe.com/doc/3.0.0/dbio.html#transactions-and-pinned-sessions
The idea is that you wrap a sequence of IO operations into a transactionally
like shown in this example:
val a = (for {
ns <- coffees.filter(_.name.startsWith("ESPRESSO")).map(_.name).result
_ <- DBIO.seq(ns.map(n => coffees.filter(_.name === n).delete): _*)
} yield ()).transactionally
val f: Future[Unit] = db.run(a)
This way Slick still process all the operations reactively, but it runs them all in one transaction sequentially.
So your example would look like this:
def doSomething(???) = (for {
_ <- userDao.doSomething(???)
_ <- addressDao.doSomething(???)
_ <- contactDao.doSomething(???)
} yield()).transactionally
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With