Grails makes it very easy to configure datasources for different environments (development, test, production) in its DataSources.groovy file, but there seems to be no facility for configuring multiple datasources in one environment. What to I do if I need to access several databases from the same Grails application?
-
I know this is an old post, but please change the accepted answer to @Sushanth CS since it is the how Grails currently works. – biniam Apr 15 '16 at 09:58
-
http://stackoverflow.com/a/36647714/2245264 has an answer for this. – biniam Apr 15 '16 at 14:56
6 Answers
Connecting different databases in different domain classes is very easy in Grails 2.x.x.
for example
development {
dataSource {//DEFAULT data source
.
.
}
dataSource_admin { //Convention is dataSource_name
url = "//db url"
driverClassName = "oracle.jdbc.driver.OracleDriver"
username = "test"
password = 'test123'
}
dataSource_users {
}
}
You can use any datasources in your domain classes by
class Role{
static mapping = {
datasource 'users'
}
}
class Product{
static mapping = {
datasource 'admin'
}
}

- 2,412
- 3
- 23
- 23
-
1As of 2.5.x this has changed. According to the manual: http://docs.grails.org/latest/guide/single.html#_multiple_datasources "If there is more than one `DataSource` in an application there is now a `dataSources { … }` configuration block to contain them all. Previously, multiple dataSource declarations were used, with an underscore and suffix on the additional datasources, e.g. `dataSource_lookup { … }`. – Pere Nov 03 '16 at 11:50
-
@Pere this is not correct: http://docs.grails.org/2.5.6/guide/single.html#multipleDatasources - as you see there it is still the underscore notation. You are probably referring to grails 3.x.x – codewandler May 04 '17 at 09:59
-
1@codewandler you're right; I don't understand how could I make such a mistake, as I even linked to the source. The right statement should be: "As of 3.2.1 this has changed. According to the manual: [docs.grails.org/latest/guide/single.html#_multiple_datasources](http://docs.grails.org/3.2.1/guide/upgrading.html#upgrading3x) (blah blah)" – Pere May 04 '17 at 10:31
-
what if a domain should be in both database but based on the user the operation will happen in either database a or database b ? – Sumon Bappi Sep 25 '19 at 05:47
If using Grails 2.0 or higher, there is no need for the plugin, it is supported natively.
http://www.grails.org/doc/latest/guide/single.html#multipleDatasources

- 29,498
- 21
- 89
- 122
There is now Grails plugin that enables the use of multiple datasources directly with Grails' GORM layer: http://burtbeckwith.com/blog/?p=70

- 2,297
- 2
- 19
- 15
-
7Note this is no longer needed as of Grails 2.0 (support for multiple datasources is built into the core framework) – Peter Mar 02 '12 at 17:06
Grails 2.0 can handle multiple data sources without a plugin:
Example with a different datasource for the dev(h2 dataSource) and test(mysql dataSource_mysql) environments:
DataSource.groovy:
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
dataSource_mysql {
dialect = org.hibernate.dialect.MySQLInnoDBDialect
driverClassName = 'com.mysql.jdbc.Driver'
username = "user"
password = "pass"
url = "jdbc:mysql://mysqldb.com/DBNAME"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
development {
dataSource {
configClass = HibernateFilterDomainConfiguration.class
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:file:../devDb;MVCC=TRUE"
sqlLogging = true
}
}
test {
dataSource_mysql {
configClass = HibernateFilterDomainConfiguration.class
dbCreate = "create" // one of 'create', 'create-drop', 'update', 'validate', ''
sqlLogging = true
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}

- 8,099
- 9
- 49
- 58

- 10,427
- 6
- 56
- 72
Do you really want to do this? In my experience, the usual scenario here is:
- An application manages its own data in its own database schema
- Often, the application will require data from other sources (for example, so reference data doesn't get copied and pasted)
I've normally always had the luxury of all the schemas residing on the one database instance. Therefore, my application:
- only has one database connection - which is to the schema it owns and has read/write access
- the other applications 'export' their data via views
- my application has read access to those views, and has a synonym for that view making it appear local
The reason behind using views is so that the application that is exposing the data
- knows explicitly that it is being exported and what is being exported
- does not expose the internal structure of the schema (so if the internal structure changes, as long as the view is correct the consuming apps don't know)
I haven't actually had to do this with a Grails application, but the approach should work.
Another approach to sharing data across applications is to create a web service to expose the data. Grails makes this easy.
Hope that helps, but this approach may not be applicable for all situations.

- 142
- 2
-
In case you are still looking into this: Using more than one Datasource in a Grails project - http://www.crjug.org/?q=node/103 – Paul Feb 22 '11 at 04:23
-
3-1 for not answering the original question, there are a variety of engineering and commercial scenarios where this is necessary. – Mark Rogers May 14 '11 at 18:02
-
+1 for bringing up a great point-- managing multiple data sources is extremely annoying, particularly if you've already written application code that assumes the domain class you're interested in accessing is part of the local database. This saves you the headache of going back through your code, removing domain relationships, and manually setting up association classes to map many-many associations. A strategy that is working well for us is what Paul suggested: maintaining a local "synonym", and then examining the different ways that it could go out of sync with the remote database. – mikermcneil Dec 20 '11 at 22:46
-
2Many of us don't have the luxury of having our apps use only one data source because we don't even get to work with a single database vendor. Apps built over the course of 20 or 30 years typically use whatever database vendor is popular at the time, so you end up with dozens of different data sources for apps of just about any level of sophistication. – Calphool Jul 28 '14 at 16:01
The following post seems to be the best source of information on the subject:
How to get mutli-dataSource in grails
It boils down to:
- Define datasource1 in DevelopmentDataSource
- Define datasource2 in resources.xml
- Write a DAO for CRUD of the domain objects using datasource2
- In hibernate.cfg.xml, list all domain objects.
Only the first datasource will have dynamic finder methods.
If its a really simple query you are after and don't mind not having the ORM features you could use Groovy SQL or the native SQL features of Hibernate.

- 732,580
- 175
- 1,330
- 1,459

- 1,660
- 3
- 15
- 17