How to load datasource configuration from external file in grails 3.1.8?

Viewed 2923

I am writing a grails 3.1.8 application. My datasource written in application.groovy file.

I want to load datasource configuration like username,password,DB from an external file. Is there any way to do it in grails 3+ versions.

Here is my datasource configuration in application.groovy:-

hibernate {
    cache {
        queries = false
        use_second_level_cache = true
        use_query_cache = false
        region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory'
    }
}

dataSource {
    pooled = true
    jmxExport = true
    dialect = 'org.hibernate.dialect.PostgreSQLDialect'
    driverClassName = 'org.postgresql.Driver'
    username = 'postgres'
    password = 'postgres'
    properties = {
        jmxEnabled = true
        initialSize = 5
        maxActive = 50
        minIdle = 5
        maxIdle = 25
        maxWait = 10000
        maxAge = 10 * 60000
        timeBetweenEvictionRunsMillis = 5000
        minEvictableIdleTimeMillis = 60000
        validationQuery = "SELECT 1"
        validationQueryTimeout = 3
        validationInterval = 15000
        testOnBorrow = true
        testWhileIdle = true
        testOnReturn = false
        ignoreExceptionOnPreLoad = true
        jdbcInterceptors = "ConnectionState;StatementCache(max=200)"
        defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED // safe default
        abandonWhenPercentageFull = 100 // settings are active only when pool is full
        removeAbandonedTimeout = 120
        removeAbandoned = true
        logAbandoned = false // causes stacktrace recording overhead, use only for debugging
    }
}

environments {
    development {
        dataSource {
            dbCreate = 'update'
            url = "jdbc:postgresql://localhost:5432/testdb"
            logSql = true
        }
    }
    test {
        dataSource {
            dbCreate = 'update'
            url = "jdbc:postgresql://localhost:5432/testdb"
            logSql = true
        }
    }
    production {
        dataSource {
            dbCreate = 'update'
            url = "jdbc:postgresql://localhost:5432/testdb"
            logSql = true
        }
    }
}
5 Answers

You can load your external configuration file from file system using the following implementation.

This example defines for each environment( development / production / test) a separate path to an external config file.

 environments {
     development {
          grails.config.locations = [
                "file:///home/<CONFIG_FILE_LOCATION_DIR>/myconfig_developement.groovy" //for Unix based systems
          ]
     }
     production {
          grails.config.locations = [
                "file:///home/<CONFIG_FILE_LOCATION_DIR>/myconfig_production.groovy" // for Unix based systems
          ]
     }
}

Put your database configuration in myconfig_developement.groovy as follows:

dataSource {
    dbCreate = 'update'
    url = "jdbc:postgresql://localhost:5432/testdb"
    logSql = true
}

You can use external-config Grails plugin and define the configuration in your external configuration file.

grails.config.locations = [
        "file:///etc/app/myconfig.groovy"
]

And then define datasource configuration in myconfig.groovy

I'm using grails 3.3.11 and here is what I did for my DB configuration to be loaded from external file/source:


application.yaml:

kept all values empty so that they can be overridden by external source

dataSource:
  pooled: true
  driverClassName: "com.microsoft.sqlserver.jdbc.SQLServerDriver"
  url:
  username:
  password:
  loggingSql: true

Now there are two ways to run an application,

  1. bootRun (configuration below)
  2. Runnable jar or war, java -jar --spring.config.location=file:external.properties

bootRun (build.gradle)

def Properties localBootRunProperties() {
    Properties p = new Properties()
    p.load(new FileInputStream("path to external.properties"))
    return p
}
bootRun {
    doFirst {
        bootRun.systemProperties = localBootRunProperties()
    }
    ...
}

Now, external.properties file should have exact same configuration to override application.yaml values

dataSource.url=
dataSource.username=
dataSource.password=

And that's it. Enjoy.

Related