(Quick Reference)

7 Service Configuration - Reference Documentation

Authors: Burt Beckwith

Version: 1.2.3

7 Service Configuration

When you provision a service and bind it to an application, an environment variable VCAP_SERVICES is set for your server instance(s). This is a JSON string and you can use it to configure your DataSource, MongoDB, and Redis connections and will look something like this (formatted for readability):


Fortunately the plugin manages this for you, so in general you don't need to be aware of this. It will update your DataSource configuration if you have a MySQL or PostgreSQL service provisioned and bound, and likewise your MongoDB and Redis connection information if you have those plugins installed and have those services provisioned and bound.

If you're not using the MongoDB or Redis plugin then you'll need to access the information yourself - just call System.getenv('VCAP_SERVICES') and parse the JSON.


In addition to replacing the username, password, and JDBC url for your DataSource, the plugin will configure database connection testing, using the following settings:

removeAbandoned = true
removeAbandonedTimeout = 300 // 5 minutes
testOnBorrow = true
validationQuery = '/* ping */ SELECT 1'

This will only be done if the DataSource is the standard org.apache.commons.dbcp.BasicDataSource that Grails configures since we assume that if you have customized the DataSource, you will configure connection testing yourself. You can also tell the plugin to not make these changes with

grails.plugin.cloudfoundry.datasource.disableTimeoutAutoconfiguration = true

in Config.groovy.

You should specify the Dialect to ensure that the correct table type is used. In MySQL InnoDB tables are preferred since MyISAM tables do not support transactions, so it's a good idea to use the MySQLInnoDBDialect. In addition you can customize the JDBC url (for example to use UTF-8). The best way to configure these is to populate the production settings (or whichever environment you're using for deployment) with these values, for example:

production {
   dataSource {
      dialect = org.hibernate.dialect.MySQLInnoDBDialect
      driverClassName = 'com.mysql.jdbc.Driver'
      username = 'n/a'
      password = 'n/a'
      url = 'jdbc:mysql://localhost/db?useUnicode=true&characterEncoding=utf8'
      dbCreate = 'update'

username and password will be replaced but can't be blank or missing, so they're set to dummy values. In addition the part of the URL to the left of the ? character will be replaced, so that just has to be valid syntax but doesn't need to be a real database. The part to the right of the ? ("useUnicode=true&characterEncoding=utf8") will be appended to the auto-generated URL.


Although there's no explicit support for Compass in Cloud Foundry, the plugin does detect that the Searchable plugin is installed and will configure it to write its Lucene index to a writeable directory on your server.