Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slick 3 Transactions

Tags:

scala

slick

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?

like image 481
Bomgar Avatar asked May 19 '15 13:05

Bomgar


1 Answers

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
like image 143
Gregor Raýman Avatar answered Sep 22 '22 23:09

Gregor Raýman