Search code examples
mysqlscalaslick

Slick silently fails when no MySQL java driver provided


If I have this in my build.sbt

libraryDependencies ++= List(
  "com.typesafe.slick" %% "slick" % slickV,
  "org.slf4j" % "slf4j-nop" % "1.6.4",
  "mysql" % "mysql-connector-java" % "5.1.40"
)

Then following code produces an expected result:

    try{
      db.run((for(visitor <- visitors) yield visitor).result.headOption).map(println(_))
    }catch{
      case e:Throwable => e.printStackTrace()
    }finally db.close()

But when I remove the last "mysql" % "mysql-connector-java" % "5.1.40" line from the dependencies then nothing ever happens. I don't get the result printed but what is interesting I don't get any error as well. Is this an expected behavior of Slick? Could you help me to clarify which driver is being used in this case and what happens? Probably the reason is in slf4j-nop, can it be that it hides any errors/warnings output?


Solution

  • First:

    Change your code into:

      db.run((
          for(visitor <- visitors) yield visitor).result.headOption).map(println(_)
      ).recover {
          case e: Throwable => e.printStackTrace()
      }.onComplete {
          case _ => db.close()
      }
    

    Second:

    I don't know where you run your code but if it is some kind of unit test of main method then you probably finish execution before above code is being run (remember db.run invokes async execution).

    I am not going into details of how you should approach actually handling this properly ( definitely naive wait/sleep is not a way to code - neither in production code nor in unit tests ). For the sake of solving your above problem however, let's just put after above code this:

    Thread.sleep(5000)
    

    and let's see if it helps. We can potentially follow up from there if there are further questions.