<p>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?</p>

[{'answer_id': 43317, 'author': 'Rich Lawrence', 'author_id': 1281, 'author_profile': 'https://Stackoverflow.com/users/1281', 'pm_score': -1, 'selected': False, 'text': '<p>The following post seems to be the best source of information on the subject:</p>\n\n<p><a href="http://www.nabble.com/How-to-get-multi-dataSource-in-grails--td12291409.html#a12322879" rel="nofollow noreferrer">How to get mutli-dataSource in grails</a></p>\n\n<p>It boils down to:</p>\n\n<ul>\n<li>Define datasource1 in DevelopmentDataSource</li>\n<li>Define datasource2 in resources.xml</li>\n<li>Write a DAO for CRUD of the domain objects using datasource2</li>\n<li>In hibernate.cfg.xml, list all domain objects. </li>\n</ul>\n\n<p>Only the first datasource will have dynamic finder methods.</p>\n\n<p>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.</p>\n'}, {'answer_id': 50357, 'author': 'Ben Williams', 'author_id': 2453, 'author_profile': 'https://Stackoverflow.com/users/2453', 'pm_score': 4, 'selected': True, 'text': '<p>There is now Grails plugin that enables the use of multiple datasources directly with Grails\' GORM layer:\n<a href="http://burtbeckwith.com/blog/?p=70" rel="noreferrer">http://burtbeckwith.com/blog/?p=70</a></p>\n'}, {'answer_id': 4918627, 'author': 'Paul', 'author_id': 591537, 'author_profile': 'https://Stackoverflow.com/users/591537', 'pm_score': 1, 'selected': False, 'text': "<p>Do you really want to do this? In my experience, the usual scenario here is:</p>\n\n<ol>\n<li>An application manages its own data in its own database schema</li>\n<li>Often, the application will require data from other sources (for example, so reference data doesn't get copied and pasted)</li>\n</ol>\n\n<p>I've normally always had the luxury of all the schemas residing on the one database instance. Therefore, my application:</p>\n\n<ul>\n<li>only has one database connection - which is to the schema it owns and has read/write access</li>\n<li>the other applications 'export' their data via views </li>\n<li>my application has read access to those views, and has a synonym for that view making it appear local</li>\n</ul>\n\n<p>The reason behind using views is so that the application that is exposing the data </p>\n\n<ol>\n<li>knows explicitly that it is being exported and what is being exported</li>\n<li>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)</li>\n</ol>\n\n<p>I haven't actually had to do this with a Grails application, but the approach should work.</p>\n\n<p>Another approach to sharing data across applications is to create a web service to expose the data. Grails makes this easy.</p>\n\n<p>Hope that helps, but this approach may not be applicable for all situations.</p>\n"}, {'answer_id': 9137003, 'author': 'Peter', 'author_id': 17123, 'author_profile': 'https://Stackoverflow.com/users/17123', 'pm_score': 4, 'selected': False, 'text': '<p>If using Grails 2.0 or higher, there is no need for the plugin, it is supported natively.</p>\n\n<p><a href="http://www.grails.org/doc/latest/guide/single.html#multipleDatasources" rel="noreferrer">http://www.grails.org/doc/latest/guide/single.html#multipleDatasources</a></p>\n'}, {'answer_id': 12075345, 'author': 'anataliocs', 'author_id': 555177, 'author_profile': 'https://Stackoverflow.com/users/555177', 'pm_score': 2, 'selected': False, 'text': '<p>Grails 2.0 can handle multiple data sources without a plugin:</p>\n\n<p>Example with a different datasource for the dev(h2 dataSource) and test(mysql dataSource_mysql) environments:</p>\n\n<p><strong>DataSource.groovy:</strong></p>\n\n<pre><code>dataSource {\n    pooled = true\n    driverClassName = "org.h2.Driver"\n    username = "sa"\n    password = ""\n}\ndataSource_mysql {\n    dialect = org.hibernate.dialect.MySQLInnoDBDialect\n    driverClassName = \'com.mysql.jdbc.Driver\'\n    username = "user"\n    password = "pass"\n    url = "jdbc:mysql://mysqldb.com/DBNAME"\n}\nhibernate {\n    cache.use_second_level_cache = true\n    cache.use_query_cache = false\n    cache.region.factory_class = \'net.sf.ehcache.hibernate.EhCacheRegionFactory\'\n}\n\n// environment specific settings\nenvironments {\n    development {\n        dataSource {\n            configClass = HibernateFilterDomainConfiguration.class\n            dbCreate = "update" // one of \'create\', \'create-drop\', \'update\', \'validate\', \'\'\n            url = "jdbc:h2:file:../devDb;MVCC=TRUE"\n            sqlLogging = true\n        }\n    }\n    test {\n        dataSource_mysql {\n            configClass = HibernateFilterDomainConfiguration.class\n            dbCreate = "create" // one of \'create\', \'create-drop\', \'update\', \'validate\', \'\'\n            sqlLogging = true\n        }\n    }\n    production {\n        dataSource {\n            dbCreate = "update"\n            url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000"\n            pooled = true\n            properties {\n               maxActive = -1\n               minEvictableIdleTimeMillis=1800000\n               timeBetweenEvictionRunsMillis=1800000\n               numTestsPerEvictionRun=3\n               testOnBorrow=true\n               testWhileIdle=true\n               testOnReturn=true\n               validationQuery="SELECT 1"\n            }\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 14894944, 'author': 'Sushanth CS', 'author_id': 363345, 'author_profile': 'https://Stackoverflow.com/users/363345', 'pm_score': 5, 'selected': False, 'text': '<p>Connecting different databases in different domain classes is very easy in Grails 2.x.x.</p>\n\n<p>for example </p>\n\n<pre><code>development {\n    dataSource {//DEFAULT data source\n      .\n      .\n    }\ndataSource_admin { //Convention is dataSource_name\n        url = "//db url"\n        driverClassName = "oracle.jdbc.driver.OracleDriver" \n        username = "test"\n        password = \'test123\'\n    }\ndataSource_users {\n\n    }\n}\n</code></pre>\n\n<p>You can use any datasources in your domain classes by </p>\n\n<pre><code>class Role{\n   static mapping = {\n      datasource \'users\'\n   }\n}\n\n class Product{\n    static mapping = {\n      datasource \'admin\'\n   }\n }\n</code></pre>\n\n<p><a href="http://healthycoder.in/multiple-data-sources-in-grails-2-0/">For more details look at this</a></p>\n'}]
['https://Stackoverflow.com/questions/41018', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2453/']