Since Grails is built on Java technology to set-up a data source requires some knowledge of JDBC (the technology that doesn't stand for Java Database Connectivity).
Essentially, if you are using another database other than HSQLDB you need to have a JDBC driver. For example for MySQL you would need
Connector/JDrivers typically come in the form of a JAR archive. Drop the JAR into your projects
lib
directory.
Once you have the JAR in place you need to get familiar Grails' DataSource descriptor file located at
grails-app/conf/DataSource.groovy
. This file contains the dataSource definition which includes the following settings:
driverClassName
- The class name of the JDBC driver
username
- The username used to establish a JDBC connection
password
- The password used to establish a JDBC connection
url
- The JDBC URL of the database
dbCreate
- Whether to auto-generate the database from the domain model or not
pooled
- Whether to use a pool of connections (defaults to true)
logSql
- Enable SQL logging
dialect
- A String or Class that represents the Hibernate dialect used to communicate with the database. See the org.hibernate.dialect package for available dialects.
properties
- Extra properties to set on the DataSource bean. See the Commons DBCP BasicDataSource documentation.
A typical configuration for MySQL may be something like:
dataSource {
pooled = true
dbCreate = "update"
url = "jdbc:mysql://localhost/yourDB"
driverClassName = "com.mysql.jdbc.Driver"
username = "yourUser"
password = "yourPassword"
}
When configuring the DataSource do not include the type or the def keyword before any of the configuration settings as Groovy will treat these as local variable definitions and they will not be processed. For example the following is invalid:
dataSource {
boolean pooled = true // type declaration results in local variable
…
}
Example of advanced configuration using extra properties:
dataSource {
pooled = true
dbCreate = "update"
url = "jdbc:mysql://localhost/yourDB"
driverClassName = "com.mysql.jdbc.Driver"
username = "yourUser"
password = "yourPassword"
properties {
maxActive = 50
maxIdle = 25
minIdle = 5
initialSize = 5
minEvictableIdleTimeMillis = 60000
timeBetweenEvictionRunsMillis = 60000
maxWait = 10000
}
}