Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
like image 477
ps0604 Avatar asked Dec 03 '25 10:12

ps0604


1 Answers

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)?

like image 140
Paul Dolega Avatar answered Dec 05 '25 02:12

Paul Dolega



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!