playframeworkslickplayframework-2.5slick-3.0play-slick

Configuring two databases in PlaySlick using injection


I'm trying to configure two databases using PlaySlick as documented here. The problem in the code below is that even though I configured a second database db2 any attempt to use it redirects the action to db1 (tries to find the table in db1 and throws an SQL exception saying that the table is not found).

How to configure two databases in PlaySlick using injection?

This is my attempt:

class ManageUsersDAO @Inject()(
    @NamedDatabase("db1") protected val dbConfigProvider: DatabaseConfigProvider,
    @NamedDatabase("db2") protected val dbConfigProvider2: DatabaseConfigProvider)
                               extends HasDatabaseConfigProvider[JdbcProfile] {
   import driver.api._

   val db1 = dbConfigProvider.get[JdbcProfile].db
   val db2 = dbConfigProvider2.get[JdbcProfile].db

And the application.conf entries:

slick.dbs.db1.driver = "slick.driver.MySQLDriver$"
slick.dbs.db1.db.driver = "com.mysql.jdbc.Driver"
slick.dbs.db1.db.url = "jdbc:mysql://localhost:3306/db1"
slick.dbs.db1.db.user = "root"
slick.dbs.db1.db.password = "db1"

slick.dbs.db2.driver = "slick.driver.MySQLDriver$"
slick.dbs.db2.db.driver = "com.mysql.jdbc.Driver"
slick.dbs.db2.db.url = "jdbc:mysql://localhost:3306/db2"
slick.dbs.db2.db.user = "root"
slick.dbs.db2.db.password = "db2"

Solution

  • First of all I don't think it makes sense to extend HasDatabaseConfigProvider (it helps, though only very little, with single db configuration; you are getting db manually either way).

    Apart from that - it looks definitely good.

    I'd rather ask a silly question - why do you think it's connecting to wrong database in the first place? My blind guess would be that it might be something wrong with your evolutions (that's why you don't have tables in your databases).

    Could you double check you if actually have this tables in db (e.g. with db command line)?