databasescalaplayframework-2.0slick-3.0play-slick

Several data source in Play 2 framework with slick


How can I configure and use several data source in Play 2.5.x Framework with Slick? For example, one data source should be MySQL and another PostgreSQL.

  slick.dbs.default.driver = "utils.db.TetraoPostgresProfile$"
  slick.dbs.default.db.driver = "org.postgresql.Driver"
  slick.dbs.default.db.url = "jdbc:postgresql://localhost:5432/templatesite_db"
  slick.dbs.default.db.user = "user"
  slick.dbs.default.db.password = "pass"

  slick.dbs.secondary.driver = "slick.jdbc.MySQLProfile$"
  slick.dbs.secondary.db.driver = "com.mysql.jdbc.Driver"
  slick.dbs.secondary.db.url = "jdbc:mysql://localhost:3306/dbName"
  slick.dbs.secondary.db.user = "root"
  slick.dbs.secondary.db.password = "pass"

Solution

  • This is directly addressed in the ScalaDatabase docummentation page:

    // inject "orders" database instead of "default"
    class ScalaInjectNamed @Inject()(
      @NamedDatabase("orders") db: Database) extends Controller {
      // do whatever you need with the db
    }
    

    https://www.playframework.com/documentation/2.5.x/PlaySlick#usage

    https://www.playframework.com/documentation/2.5.x/ScalaDatabase#obtaining-a-jdbc-connection