Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally connect to a database

To access a database one has to specify the DB details in the conf file.

If the DB is up everything works fine.

If the DB is down Play throws an exception, which I cannot control.

I would like to conditionally connect to a DB. Lets say I only want to connect to a DB if a flag is set somewhere. Is there a more manual way to connect to a DB in Play?

Update: I guess I haven't been very clear.

I want the App to not fail if the DB is down and fetch data from other alternate sources. How can I accomplish this in PLAY?

like image 881
rahul Avatar asked May 30 '12 16:05

rahul


1 Answers

I assume you want to start your app without a DB for development? You can pass an alternative config file on startup where you for example configure a in memory db:

start -Dconfig.resource=development.conf

edit:

You can configure several databases in your app config and get them with DB.getDataSource(name: String) or get a connection to it with DB.getConnection(name: String) or even run a transaction with DB.getConnection[A](name: String)(f: Connection => A)

see: http://www.playframework.org/documentation/api/2.0/scala/index.html#play.api.db.DB$

like image 168
drexin Avatar answered Sep 19 '22 06:09

drexin