Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Scala: How to access multiple databases with anorm and Magic[T]

I want to access two databases in Play Scala with anorm and Magic[T], (one is H2 and another is PostgreSQL). I just don't know how to config it...

I noticed that we can set another database connection in conf/application.conf

db_other.url=jdbc:mysql://localhost/test
db_other.driver=com.mysql.jdbc.Driver
db_other.user=root
db_other.pass=

However, how can I use it with Magic? (I read the source code of Magic but don't understand it... my am a freshman of Scala)

Anyhow, if multiple database access is impossible with Magic[T] , I wish to do it with anorm, then how can I config it?

var sqlQuery = SQL(          //I guess some config params should be set here, but how?
    """
      select * from Country
    """
)
like image 572
Wint Avatar asked Nov 04 '22 10:11

Wint


1 Answers

In play.api.db.DB it appears you can pass in a string of the name you defined in application.conf.

Then use one of the methods specified here: http://www.playframework.org/documentation/2.0/ScalaDatabase

# play.api.db.DB.class

def withConnection[A](name : scala.Predef.String)(block : scala.Function1[java.sql.Connection, A])(implicit app : play.api.Application) : A = { /* compiled code */ }
like image 124
vaughan Avatar answered Nov 09 '22 14:11

vaughan