Search code examples
mysqlscalaslick

Can't make Scala, Slick and MySQL work together


I'm trying to make these three work together but can't. Slick documentation is lacking. I have application.conf setup for Typesafe config like this :

mysql = {
  url = "jdbc:mysql://localhost/slickdb"
  slick.driver=scala.slick.driver.MySQLDriver
  driver=com.mysql.cj.jdbc.Driver
  properties = {
    user = root
    password = null
  }
  connectionPool = true
  keepAliveConnection = true
}

and build.sbt relevant dependencies :

libraryDependencies ++= Seq(
   ...
   "org.eclipse.jetty" % "jetty-webapp" % "9.2.15.v20160210" % "container",
   "javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided",
   "com.typesafe.slick" %% "slick" % "3.2.0",
   "com.typesafe.slick" %% "slick-hikaricp" % "3.2.0",
   "mysql" % "mysql-connector-java" % "latest.integration"
   ...
)

and try to compile a simple DAO

import slick.jdbc.MySQLProfile.api._
class testDAO {

  private val db = Database.forConfig("mysql")
} 

but SBT compiler throws me this exception :

java.lang.ClassNotFoundException: true
        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:17)
        at slick.jdbc.JdbcDataSource$.loadFactory$1(JdbcDataSource.scala:37)
        at slick.jdbc.JdbcDataSource$.forConfig(JdbcDataSource.scala:46)

I also tried :

import slick.jdbc.MySQLProfile.api._
import slick.basic.DatabaseConfig

class testDAO {
  val dbConfig = DatabaseConfig.forConfig("mysql") 
}

but I get another Exception :

slick.SlickException: Configured profile com.mysql.cj.jdbc.Driver does not conform to requested profile scala.runtime.Nothing$
        at slick.basic.DatabaseConfig$.forConfig(DatabaseConfig.scala:99)
        at com.unamur.service.testDAO.<init>(CatDAO.scala:18)
        at com.unamur.app.TestServlet.<init>(TestServlet.scala:11)
        at ScalatraBootstrap.init(ScalatraBootstrap.scala:7)

Anyone has an idea ? Thanks in advance.


Solution

  • connectionPool = true is the problem here. You need to provide implementation, HikariCP or something else.