Search code examples
databasepostgresqlscalaplayframeworkslick

Play Scala Slick transactions


I want to use transactions in Play Slick Scala

I have three different methods which returns boolean value

def updateFirst() 
def updateSecond()
def updateThird()

I am trying to run the below command but it seems transactionally command is not found

val operations =
  for {
     _ <- updateFirst()
     _ <- updateSecond()
     _ <- updateThird()
  } yield ()

sync(db.run(operations).transactionally))

My codebase sbt version related to slick is as below :

"com.typesafe.play" %% "play-slick" % "2.0.0",
"com.typesafe.play" %% "play-slick-evolutions" % "2.0.0",
"com.typesafe.slick" %% "slick" % "3.0.1",

Even DBIO is not showing anything.

Any help or lead is appreciated.


Solution

  • Take a look at this answer: https://stackoverflow.com/a/41624229/2239369

    It has been covered multiple times already - you can only compose operations in transaction as long as they return DBIO[T] (where T is your resulting type). In your case your methods should return:

    def updateFirst(): DBIO[Boolean] 
    def updateSecond(): DBIO[Boolean]
    def updateThird(): DBIO[Boolean]
    

    Also your last line should be:

    sync(db.run(operations.transactionally))
    

    (transactionally is called on DBIO, not on Future )

    Also take a look at following slide: http://slides.com/pdolega/slick-101#/85