Grails - DataSources New

Grails Data Sources 0.6 and above

Since Grails 0.6 the data sources have been unified into a single file called grails-app/conf/DataSource.groovy an example of which can be seen below:

dataSource {
    pooled = false
    loggingSql = true
    driverClassName = "org.hsqldb.jdbcDriver"
    username = "sa"
    password = ""
    dbCreate = "create-drop" // one of 'create', 'create-drop','update'
    url = "jdbc:hsqldb:mem:devDB"
}

The available properties to set are:

  • pooled - Whether to use a pooled connection or not
  • driverClassName - The name of the JDBC driver to use
  • username - The username used to establish a connection
  • password - The password used to establish a connection
  • dbCreate - Whether to generate the database at runtime or not. Possible options are "create", "create-drop" and "update"
  • url - The URL of the database
  • loggingSql - Set to true if you want to see SQL logs in the console
  • dialect - The Hibernate dialect to use to communicate with the database. Normally can be left blank and it is auto-detected

Environment specific settings

You can specific environment specific settings using the "environments" block:

dataSource {
   pooled = false
   driverClassName = "org.hsqldb.jdbcDriver"
   username = "sa"
   password = ""
}
environments {
   development {
      dataSource {
         dbCreate = "create-drop"
         url = "jdbc:hsqldb:mem:devDB"
      }
   }
   production {
      dataSource {
          dbCreate = "update"
          url = "jdbc:hsqldb:file:prodDb;shutdown=true"
      }
   }
}

Using other databases

Grails GORM is based on the Hibernate object/relational mapping framework. Therefore Grails supports all Hibernate databases.

Hibernate is regularly tested with the following SQL databases:

  • DB2 7.1, 7.2, 8.1
  • HSQL DB
  • HypersonicSQL 1.61, 1.7.0, 1.7.2, 1.8
  • Microsoft SQL Server 2000
  • MySQL 3.23, 4.0, 4.1, 5.0
  • Oracle 8i, 9i, 10g
  • PostgreSQL 7.1.2, 7.2, 7.3, 7.4, 8.0, 8.1
  • SAP DB 7.3
  • Sybase 12.5 (JConnect 5.5)
  • Timesten 5.1
Hibernate has also been tested with and is believed to be compatible with current versions of:
  • Apache Derby
  • HP NonStop SQL/MX 2.0 (requires Dialect from HP)
  • Firebird (1.5 with JayBird 1.01 tested)
  • FrontBase
  • Informix
  • Ingres
  • Interbase (6.0.1 tested)
  • Mckoi SQL
  • Pointbase Embedded (4.3 tested)
  • Progress 9

Using MySQL

The following piece of code uses the "pooled" attribute. I have no idea why this works instead of pooling, but it does for me. If things are not working out with "pooled"/"pooling" switch between the two to see what does work for you.

dataSource 
{
	pooled = true
	driverClassName = "com.mysql.jdbc.Driver"
	username = "root"
	password = "admin"
	dbCreate = "create-drop"
	url = "jdbc:mysql://localhost/database"
}