I\'m trying to get familiar with Slick 3.0 and Futures (using Scala 2.11.6). I use simple code based on Slick\'s Multi-DB Cake Pattern example. Why does the following code t
I think that something is not correct in what you are trying to do: Slick's run
method doesn't return Unit
and doesn't fail with an exception - as it used to in previous versions. run
now returns a Future
, so if you want to run actions in sequence you need to flatMap
the steps, or use a for-comprehension:
def init() = {
val = results for {
_ <- db.run(dal.create)
_ <- db.run(dal.stuffTable += Stuff(23, "hi"))
r <- db.run(dal.stuffTable.filter(_.serial === 23).result)
} yield r
}
I am not sure that you really need to use db.close
that way: that is actually what may be causing the error (i.e. the db is closed in concurrence with the future that runs the actual queries so the execution can't happen).
If you want to handle errors use Future
's capabilities, e.g.:
result.onFailure { case NonFatal(ex) => // do something with the exception }