Grails - Configuring the Database Connection
All database connections are now in the file grails-app/conf/DataSource.groovy saved. A typical configuration for connecting to a mysql data using JDBC (J ava D ata b ase onnectivity C) might look like this:
A 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | dataSource { pooled = true driver class name = "com.mysql.jdbc.Driver" username = "username" password = "password" } {hibernate true Cache. use_second_level_cache = true true Cache. use_query_cache = true 'org.hibernate.cache.EhCacheProvider' Cache. provider_class = 'org.hibernate.cache.EhCacheProvider' } / / Environment specific settings environments { development { dataSource { / / One of 'create', 'create-drop', 'update' dbCreate = "update" url = "jdbc: mysql :/ / localhost: 3306/usermanagement" logSql = true } } test { dataSource { dbCreate = "update" url = "jdbc: mysql :/ / localhost: 3306/usermanagement_test" } } production { dataSource { dbCreate = "update" url = "jdbc: mysql :/ / localhost: 3306/usermanagement_prod" } } } |
The parameter pooling states whether a connection pool to be used for database connections, or not. By default, the implementation of the Connect pool is Apache Commons project is used.
The parameter dbCreate says that the database structure should be generated. For the included "In Memory - Database" Hypersonic by default the value to "create-drop" is set. This means that the database will be removed from each test run and re-created.
Hand-curated databases, such as our MySql database should set the value "update" have. The "Production Data Source", this parameter should be commented out entirely.
To test the configuration, only the JDBC driver (in our case, Connector / J from MySQL) to the directory \lib and copy the application itself with grails run-app to start. By default, this command used the development data bank, but you can also specify other parameters as a special data source:
grails dev run-app grails prod run-app grails test run-app
The same applies to the command grails war , with the web application into a Web Archive (*. war file) is packed.
Other parameters within the configuration are as logSql and dialect , with which the logging of SQL commands on and off and a special set of SQL commands for the Hibernate configuration can be specified.
logSql = true dialect = MySQLDialect.class
The last remains of a JNDI DataSource configuration.
To declare a JNDI DataSource must be in the respective servlet container (eg Tomcat) or Application Server (eg JBoss) before it can be configured in Grails.
A 2 3 4 5 6 7 8 9 10 | dataSource { / / Common settings here } environments { production { dataSource { url = "jdbc: mysql :/ / liveip.com / liveDb" } } } |
A good guide for the definition of a JNDI DataSource provides the Tomcats in the Tomcat documentation .




August 31st, 2008 at 1:36 pm
[...] The current version of the article I have for better maintainability as part of a small tutorial on Grails [...]