Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing postgres using slick is not working

I have following environment scala2.11.8 / akka 2.4.8 / slick 3.1.1 / postgreSQL 9.6

I have done following configuration in application.conf

mydb {
  driver = "slick.driver.PostgresDriver$"
  db {
    url = "jdbc:postgresql://localhost:5432/mydb"
    driver = org.postgresql.Driver
    user="postgres"
    password="postgres"
    numThreads = 10
    connectionPool = disabled
    keepAliveConnection = true
  }
}

The DB access is done in class

package mib
import slick.driver.PostgresDriver.api._
import scala.concurrent.ExecutionContext.Implicits.global
class DBAccess {
  import scala.concurrent.Future
  import scala.concurrent._
  import scala.concurrent.duration._
  import slick.backend.DatabaseConfig
  import slick.driver.JdbcProfile
  import slick.driver.PostgresDriver
  import slick.driver.PostgresDriver.api._  
  import slick.jdbc.JdbcBackend.Database

  println("creating database")
 val dbConfig: DatabaseConfig[PostgresDriver] = DatabaseConfig.forConfig("mydb")
 val db = dbConfig.db
 try{
 val accesspoints = TableQuery[mibPoint] 
  // SELECT * FROM users WHERE username='john'
  val q = for (a <- accesspoints) yield a.mib_id
  val dbAction = q.result
  val f: Future[Seq[String]] = db.run(dbAction)
  Await.result(f, Duration.Inf)
  f.onSuccess { case s => println(s"Result: $s") }
  }
  catch
  {
    case _: Throwable =>println("got some exception")
  }
  finally 
    db.close
}

// this is a class that represents the table I've created in the database
  class mibPoint(tag: Tag) extends Table[(String, Double,Double)](tag, "mib_non_info") {
    def mac_id = column[String]("mib_id",O.PrimaryKey)
    def lat = column[Double]("lat")
    def lng = column[Double]("lng")
    def * = (mib_id, lat,lng)
  }

This class is called from APP object as

object wmib extends App {    
  val mWBootStrapper =  new bootStrap
  mWBootStrapper.ReadProperties();
  val mdB  = new DBAccess 
}

However after running, I always get the output as "got some exception" I have tried to enable logging using slf4j/logback but still i do not see much in the logs. The above seems like very trivial and probably i am missing something obvious. Thanks in advance, Vishal

I added the exception handling as suggested by sarvesh. That was cool and thank you. However my problem vanished and there was no exception. What happened? Earlier in the day, I had attempted to access the DB using the java JDBC way. i.e. just to check that there is nothing wrong with DB and DB access. In the process, I downloaded and added the postgresDriver in the classpath. Earlier that was not the case. Since the driver was now in the path, the code just worked. Since I was not printing the exception, i was not realizing the error. I then removed the driver jar AND i got the following error.

01:44:08.224 [mydb.db-1] DEBUG slick.jdbc.JdbcBackend.statement - Preparing statement: select "mib_id" from "mibpoint"
01:44:08.224 [mydb.db-1] DEBUG slick.jdbc.DriverDataSource - Driver org.postgresql.Driver not already registered; trying to load it
java.lang.ClassNotFoundException: org.postgresql.Driver
    at java.lang.ClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at slick.util.ClassLoaderUtil$$anon$1.loadClass(ClassLoaderUtil.scala:12)
    at slick.jdbc.DriverDataSource$$anonfun$init$2.apply(DriverDataSource.scala:60)
    at slick.jdbc.DriverDataSource$$anonfun$init$2.apply(DriverDataSource.scala:58)
    at scala.Option.getOrElse(Option.scala:121)

Thanks to all for helping. Vishal

like image 712
vishal Avatar asked Jun 05 '26 03:06

vishal


2 Answers

I was running into the same connection issues when first using Slick. I submitted this PR with details on how to connect up a local Postgres server.

https://github.com/slick/slick/issues/1861#issuecomment-387616310.

But basically try edit your build.sbt and application.conf files:

postgres setup for slick

like image 107
Antonio de Perio Avatar answered Jun 06 '26 16:06

Antonio de Perio


The 2020 answer:

You have to make sure of two things:

  1. Add the driver to the build.sbt's libraryDependencies: "org.postgresql" % "postgresql" % "42.2.5". That will cause java.sql.DriverManager's method getDrivers (which is used by slick in class DriverDataSource) to find the driver org.postgresql.Driver
  2. Make sure that the database url in application.conf is following the JDBC's full-url pattern, as described in the source code: https://github.com/slick/slick/blob/42d787b4950fe876569b5fd68e98c4e0379ac83c/slick/src/main/scala/slick/jdbc/DatabaseUrlDataSource.scala#L9. For example: postgresql://user:password@localhost:5432/postgres.

My full configuration is:

build.sbt

libraryDependencies ++= Seq(
      ...,
      "org.postgresql" % "postgresql" % "42.2.5"
    )

application.conf

slick-postgres {
  profile = "slick.jdbc.PostgresProfile$"
  db {
    dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
    properties = {
      driver = "org.postgresql.Driver"
      url = "postgresql://postgres:postgres@localhost:5432/postgres"
    }
  }
}
like image 24
nbtk Avatar answered Jun 06 '26 16:06

nbtk