Search code examples
playframeworktypesafe-confighocon

Overriding a Database object in application.conf to no-op in certain environments


I am trying to setup an application that has multiple database connections (some for readonly and some for readwrite).

95% of the time I would like both to be configured and, as such, I've got the environment variables specified appropriately to allow this to be the case.

However, in a few environments, I would like to disable one of the database connection pools from being available and I cannot figure out how to do that other than to exclude that pool by default and, in the 95% of environments, specify it manually.

Here's what I have right now in my application.conf:

db.neo4j = {
    driver: org.neo4j.jdbc.Driver
    ...
}

Is there a way that I can run play so that when I do this: ./bin/run-app that I can specify NOT to load that neo4j database connection?

I've tried all of the following to no avail:

./bin/run-app -Ddb.neo4j=null
./bin/run-app -Ddb.neo4j={}
./bin/run-app -Ddb.neo4j=None

Every time I run that, I get the error neo4j has type STRING rather than OBJECT.

Thank you!


Solution

  • You can use multiple .conf files and specify exactly which one to use when starting your application. Per instance:

    ./bin/run-app -Dconfig.resource=prod.conf
    

    So, you can have a conf file that has all the configurations necessary and another one that does not configure db.neo4j at all. To avoid repetition, you can have a file that contains all the configuration (excluding db.neo4j) and then have another one that just includes the db.neo4j:

    File conf/prod.conf:

    include "application.conf"
    
    db.neo4j = {
        driver: org.neo4j.jdbc.Driver
        ...
    }