Note that there are some explanatory texts on larger screens.

plurals
  1. PODefer connection to datasources defined in application.conf
    text
    copied!<p>Say I have a database called "awesome" which is located on a live server and at the same time duplicated on a staging server for testing. My web app is based on Play 2.1.1 using Scala.<br> So I have these datasources defined in my <em>application.conf</em> file:</p> <pre><code>db.awesome-test.driver= com.mysql.jdbc.Driver db.awesome-test.url="jdbc:mysql://127.0.1.1/awesome" db.awesome-test.user=mr_awesome_tester db.awesome-test.password=justtesting db.awesome-live.driver= com.mysql.jdbc.Driver db.awesome-live.url="jdbc:mysql://127.0.0.1/awesome" db.awesome-live.user=mr_awesome db.awesome-live.password=omgthisisawesome </code></pre> <p>Depending on what environment I am on, I would like to use either <code>DB.withConnection("awesome-test")</code> or <code>DB.withConnection("awesome-live")</code>. I am controlling this via another value in my config; so I e.g. put <code>environment=awesome-live</code> in there and then get the respective connection string via <code>Play.configuration</code>.</p> <p>Now, the problem is that apparently play attempts to create a DB connection to each datasource defined in the config right away. A) This fails depending on which environment I am on. E.g. on the staging machine I will get something like this (pic is only a mock-up of course) because the live DB is not reachable:</p> <p><img src="https://i.stack.imgur.com/buWMC.png" alt=""></p> <p>...although it is completely unnecessary to try to connect to that DB, because it will never be used in this environment. B) Even if the connection would work, of course it would not be feasable to create two connections (live and testing) when only one of the two is ever needed.</p> <p>Is there a way to tell Play to defer/postpone creation of the DB connection until it is actually needed (e.g. when <code>DB.getConnection("...")</code> or <code>DB.withConnection("...")</code> or something is called for that datasource)? I am thinking something like <code>db.awesome-live.deferCreation=true</code>.</p> <p>Cheers, Alex</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload