Conditionally connect to the database

To access the database, you must specify the database data in the conf file.

If the database works, everything works fine.

If the database does not work, playback throws an exception that I cannot control.

I would like to conditionally connect to the database. Suppose I only want to connect to the database if a flag is set somewhere. Is there a more manual way to connect to the database in Play?

Update: I think I was not very clear.

I want the application not to be interrupted if the database is down and retrieving data from other alternative sources. How can I accomplish this in PLAY?

+6
source share
1 answer

I assume you want to run an application without a database for development? You can transfer an alternative configuration file at startup, where you, for example, configure db in memory:

start -Dconfig.resource=development.conf 

edit:

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

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

+1
source

Source: https://habr.com/ru/post/917032/


All Articles