Knows Not Much
Knows Not Much

Reputation: 31526

Connecting Slick 3.2.1 to MySQL using slick.jdbc.MySQLProfile

There are 100s of threads on SO on how to connect slick to Mysql and all of them use "slick.driver.MySQLDriver$". I believe that this class is now deprecated and has been replaced by "slick.jdbc.MySQLProfile"

This has been stated by the product documentation here

http://slick.lightbend.com/doc/3.2.1/api/#slick.driver.package

So in order to use the new class I define my configuration as

mysql = {
  driver = "slick.jdbc.MySQLProfile"
  properties = {
    driver = "com.mysql.cj.jdbc.Driver"
    url  = "jdbc:mysql://localhost:3306/foo"
    user = "foo"
    password = "bar"
  }
}

Database.forConfig("mysql")

But I get an exception

java.lang.RuntimeException: Failed to load class of driverClassName slick.jdbc.MySQLProfile
        at com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:323)
        at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.$anonfun$forConfig$3(HikariCPJdbcDataSource.scala:31)
        at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.$anonfun$forConfig$3$adapted(HikariCPJdbcDataSource.scala:31)
        at scala.Option.map(Option.scala:146)
        at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.forConfig(HikariCPJdbcDataSource.scala:31)

I tried many other combinations but nothing seems to work for the new class. So what is the right way to connect now when slick.driver.MySQLDriver$ is deprecated?

Here is my libraryDependencies from build.sbt

"com.typesafe.slick" %% "slick" % "3.2.1",
"com.typesafe.slick" %% "slick-hikaricp" % "3.2.1",
"com.typesafe.slick" %% "slick-codegen" % "3.2.1",
"mysql" % "mysql-connector-java" % "5.1.34",

yes I do need the connection pool.

Edit: Based on suggestion below I changed my config to

mysql = {
  profile = "slick.jdbc.MySQLProfile$"
  properties = {
    driver = "com.mysql.cj.jdbc.Driver"
    url  = "jdbc:mysql://local:3306/foo"
    user = "foo"
    password = "bar"
  }
}

but now I get error

java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
        at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:786)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:67)
        at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.forConfig(HikariCPJdbcDataSource.scala:58)
        at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.forConfig(HikariCPJdbcDataSource.scala:21)

Upvotes: 3

Views: 8451

Answers (3)

mduf
mduf

Reputation: 69

Using HikariCP Pool

application.conf

slick {
  profile = "slick.jdbc.MySQLProfile$"
  db {
    connectionPool = "HikariCP"
    url = "jdbc:mysql://127.0.0.1/abc"
    user = "user"
    password = "passabc"
    driver = "com.mysql.cj.jdbc.Driver"
    numThreads = 5
    maxConnections = 5
    minConnections = 1
  }
}

build.sbt

"com.typesafe.slick" %% "slick" % slickVersion,
"com.typesafe.slick" %% "slick-hikaricp" % slickVersion,
"mysql" % "mysql-connector-java" % "8.0.15",

Upvotes: 2

Knows Not Much
Knows Not Much

Reputation: 31526

Here is the full working config if anyone needs it

mysql = {
  profile = "slick.jdbc.MySQLProfile$"
  dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
  properties = {
    driver = "com.mysql.cj.jdbc.Driver"
    url  = "jdbc:mysql://localhost:3306/foo"
    user = "foo"
    password = "bar"
  }
}

full build.sbt

"com.typesafe.slick" %% "slick" % "3.2.1",
"com.typesafe.slick" %% "slick-hikaricp" % "3.2.1",
"com.typesafe.slick" %% "slick-codegen" % "3.2.1",
"mysql" % "mysql-connector-java" % "5.1.34",

connection established using

Database.forConfig("mysql")

Upvotes: 12

mthmulders
mthmulders

Reputation: 9705

The documentation suggests to put this in your config:

profile = "slick.jdbc.MySQLProfile$"

Note the $ at the end of the line.

Also, add the following to enable connection pooling:

dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"

Upvotes: 4

Related Questions