How to configure Slick 3.1.1 for PostgreSQL? It se

2019-07-24 17:48发布

I have two days trying to run a plain SQL query using Slick 3.1.1, I followed the Getting Started and Database Configuration guides.

The issue is that while the code compiles properly, it is ignoring my database parameters (the ones in the config are wrong), it prints this to StdOut (while it should throw an exception):

HikariCP pool database is starting.
List()

Here is my test code:

import slick.driver.PostgresDriver.api._
import scala.concurrent.ExecutionContext.Implicits.global

object app extends App {
  doit
  def doit {
    val db = Database.forConfig("mydb")
    val result = db.run( sql"SELECT NOW()".as[String] )
    println(result)
  }
}

Here is 'application.conf'

mydb = {
  dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
  properties = {
    databaseName = "mydb"
    user = "myuser"
    password = "secret"
  }
  numThreads = 10
}

Here is 'build.sbt'

libraryDependencies ++= Seq(
  "org.postgresql" % "postgresql" % "9.4.1208",
  "mysql" % "mysql-connector-java" % "5.1.35",
  "com.typesafe.slick" %% "slick" % "3.1.1",
  "com.typesafe.slick" %% "slick-hikaricp" % "3.1.1",
  "com.zaxxer" % "HikariCP" % "2.4.7"
)

Also, I have scala "2.11.8".

1条回答
▲ chillily
2楼-- · 2019-07-24 18:45

The issue was not with the configuration but how I was expecting the things to work.

I expected an exception while configuring the database with wrong credentials and this line:

val result = db.run( sql"SELECT NOW()".as[String] )

It doesn't throw any expection because db.run returns a Future, the program finishes just after this and the query is never executed, using Await.result let the Future finish the job will throw the exception for wrong configuration or return the proper value, here is a sample code:

import scala.concurrent.Await
import scala.concurrent.duration.Duration

println( Await.result(result, Duration.Inf) )
查看更多
登录 后发表回答