synchronously query ?

66 views
Skip to first unread message

techlan...@gmail.com

unread,
Jul 12, 2017, 6:06:33 AM7/12/17
to Slick / ScalaQuery
hi all 
i need in my program some times call query synchronously ?
is this solution is correct ?
can i use Await.result with all DB calls, to wait at that point until they complete, for example: Await.result(db.run(insertEffect), Duration.Inf) ?


 

Justin du coeur

unread,
Jul 12, 2017, 8:26:49 AM7/12/17
to Slick / ScalaQuery
You *can*, technically, but it's usually a Bad Idea -- it winds up doing unhappy things to the threading, and can make the program much less efficient; it undoes one of the key advantages of Slick.  It's not recommended if it's at all avoidable.

What's the reason why you need to query synchronously?  Usually when something like this comes up, it means that broader restructuring is appropriate; that isn't always easy, but it usually results in a better program if it's possible...

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/cf691203-11af-4036-9e66-f8006efe683b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

techlan...@gmail.com

unread,
Jul 12, 2017, 12:31:45 PM7/12/17
to Slick / ScalaQuery
I don't need everywhere async query ?

techlan...@gmail.com

unread,
Jul 12, 2017, 12:33:02 PM7/12/17
to Slick / ScalaQuery
simple example ?

def coffeesPrice():Double={
var sum=0
sum=sum+sumPrice()
if (sum>1000){
....example tax process
}
}

def sumPrice(){
val q = coffees.map(_.price)
val action = q.result
val result: Future[Seq[Double]] = db.run(action)
........

Justin du coeur

unread,
Jul 12, 2017, 1:11:45 PM7/12/17
to Slick / ScalaQuery
Right -- that's about what I expected.  The thing is, that code, and the stuff calling it, should be Future-based instead.  So in your example, coffeesPrice() should be returning Future[Double], not Double.  Your example is incomplete (and I'm not quite certain what it's supposed to be doing), so I can't give a complete rewrite, but it ought to look *something* like:

def coffeesPrice():Future[Double] = {
  getPrices().map { prices =>
    prices.sum()
  }
}

def getPrices():Future[Seq[Double]] = {
  val q = coffees.map(_.price)
  val action = q.result
  db.run(action)
}

It's Futures all the way up -- by and large, you never Await on the result of one of these calls, you map over it instead.  This means that the calling code needs to get smart about Futures, and that *does* sometimes mean fixing a lot of code.  But it's the efficient and honest way to operate, and newer frameworks (such as Play) are designed to be built mainly on top of Futures...

Naftoli Gugenheim

unread,
Jul 27, 2017, 12:57:44 PM7/27/17
to Slick / ScalaQuery

Often it's appropriate to return a DBIO[T] rather than a Future[T], then the code is decoupled from the database instance.


--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/CAO26FW1-ROK2WNP8HnNfyh8Fp01j%3DBHNPojtftcn3PSpE%2B0Mbw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages