Re: Slick 3.0 (scala) queries don't return data till they are run multiple times (I think)

92 views
Skip to first unread message

Naftoli Gugenheim

unread,
Jun 28, 2015, 6:52:13 AM6/28/15
to scala...@googlegroups.com
How are you determining when it returned the data?

I suspect you're misusing futures. db.run immediately returns a scala.concurrent.Future, which represents the value that will be provided when it's ready. To use it, you have to work "within" futures. (For testing you can use Await.result.)


On Thu, Jun 25, 2015 at 5:24 PM A_Student <95matc...@gmail.com> wrote:
So I'm very (extremely) new to Databases and slick and scala, so I was using the example code from their documentation at http://slick.typesafe.com/doc/3.0.0/gettingstarted.html

My problem is that for some reason, I have to run a query multiple times before it returns data. I have to rerun it atleast 3-4 times before it returns results. I use a for-loop to rerun the query and they don't necessarily give me the exact same results each time either.



to create two tables as followed:

      class Patients(tag: Tag) extends Table[(String, String, Int, String)](tag, "Patientss") {
        def PID = column[String]("Patient Id", O.PrimaryKey)
        def Gender = column[String]("Gender")
        def Age = column[Int]("Age")
        def Ethnicity = column[String]("Ethnicity")
        def * = (PID, Gender, Age, Ethnicity)
      }
      val patientsss = TableQuery[Patients]
      class DrugEffect(tag: Tag) extends Table[(String, String, Double)](tag, "DrugEffectss") {
        def DrugID = column[String]("Drug ID", O.PrimaryKey)
        def PatientID = column[String]("Patient_ID")
        def DrugEffectssss = column[Double]("Drug Effect")
        def * = (DrugID, PatientID, DrugEffectssss)
        def Patient = foreignKey("Patient_FK", PatientID, patientsss)(_.PID)}
     val d_effects = TableQuery[DrugEffect]
 

I then create these tables using

      val create_empty = DBIO.seq((patientsss.schema ++ d_effects.schema).create)
      val setup_1 = db.run(create_empty)

I have actual data in two text files, which I parse through using a buffered reader.
I store all the Drug ID's in a list creatively named `DrugIds`

Then, I start filling in the tables in the following way

I first fill in the Patients table:

       while (switch != 1) {
        val Patient = CurPatient.split("\\s+")
        if (Patient(2).toUpperCase() == "NA" || (Patient(2).toFloat % 1 != 0))
          age = -1
        else age = Patient(2).toInt
         
        val insertPatient: DBIO[Option[Int]] = patientsss ++= Seq(
          (Patient(0), Patient(1), age, Patient(3))
        )       
        var future = db.run(insertPatient)
  

        CurPatient = PatientReader.readLine()
        if (CurPatient == null)
          switch = 1 //switch to 1
      }


For the DrugEffects table, I do the following:

     while (switch != 1) {
        val Effect = CurEffect.split("\\s+")
        for (i <- 1 until DrugIds.size - 1) {
          if (Effect(i).toUpperCase() == "NA")
            d_ef = -1.00
          else d_ef = (Effect(i).toFloat).asInstanceOf[Double]
        
          val insertEffect: DBIO[Option[Int]] = d_effects ++= Seq(
            (DrugIds(i), Effect(0), d_ef)
          )
          var future2 = db.run(insertEffect)
        }

        CurEffect = EffectReader.readLine()
        if (CurEffect == null)
          switch = 1
      }
Then I run a query with the following piece of code
    val q1 = for {
        c <- patientsss
      } yield (c.PID, c.Gender, c.Age, c.Ethnicity)
      db.stream(q1.result).foreach(println)

This should just give me all the data in the Patient's table, but it doesn't necessarily do that.

Sometimes, I get the following error (but not always):

    java.util.concurrent.RejectedExecutionException: Task slick.backend.DatabaseComponent$DatabaseDef$$anon$3@47089c2c rejected from java.util.concurrent.ThreadPoolExecutor@6453123[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 215]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)
at scala.concurrent.impl.ExecutionContextImpl$$anon$1.execute(ExecutionContextImpl.scala:136)
at slick.backend.DatabaseComponent$DatabaseDef$class.scheduleSynchronousStreaming(DatabaseComponent.scala:253)
at slick.jdbc.JdbcBackend$DatabaseDef.scheduleSynchronousStreaming(JdbcBackend.scala:38)
at slick.backend.DatabaseComponent$BasicStreamingActionContext.restartStreaming(DatabaseComponent.scala:516)
at slick.backend.DatabaseComponent$BasicStreamingActionContext.request(DatabaseComponent.scala:531)
at slick.backend.DatabasePublisher$$anon$3$$anonfun$onNext$2.apply(DatabasePublisher.scala:50)
at slick.backend.DatabasePublisher$$anon$3$$anonfun$onNext$2.apply(DatabasePublisher.scala:49)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:121)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.pollAndExecAll(ForkJoinPool.java:1253)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1346)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

If I run a more complex query, the data I get back is accurate to the parameters of the query, but the same problems occur, which is that the results are either duplicated or non-existent or not complete (when I rerun the query multiple times).

Explain like I'm 5 if you can, or point me to a resource that can help me solve these problems

--

---
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/5ee0a5ca-1cd2-4761-8050-34a5a2c33d4f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

A_Student

unread,
Jun 29, 2015, 1:04:56 PM6/29/15
to scala...@googlegroups.com
Hey,

I got it to work by using wrapping db calls in an Await statement
Reply all
Reply to author
Forward
0 new messages