In my one of assignment, requirement was to support multiple datasources for the application. I searched through the web for the same and found that Grails 2.X.X support the multiple datasources out of the box. You just have to configure it in your DataSource.groovy and code looks like:
development { dataSource { dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', '' url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000" } dataSource_master { dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', '' url = "jdbc:h2:mem:masterDb;MVCC=TRUE;LOCK_TIMEOUT=10000" } dataSource_clientname1 { dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', '' url = "jdbc:h2:mem:com1Db;MVCC=TRUE;LOCK_TIMEOUT=10000" } dataSource_clientname2 { dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', '' url = "jdbc:h2:mem:com2Db;MVCC=TRUE;LOCK_TIMEOUT=10000" } }
Note : All datasoure name must have the prefix “dataSource_”.
Hope this help.
if there is any association between the 2 domains in multiple dbs how u will add the associations in domain???
I don’t think it is possible here. We do query against a single Datasource at a time. Can you please help me to understand the use-cases? I think there must be some other way to fulfill your requirement.
Hi,
In my present project I need to use 2 dbs, one for Master data and other for client data.As of now we are using single db but we planned to split into 2 dbs (master db & client db). We have association between client and master db in domains level. I tried with grails 2.X multiple data-source option, but association is not possible. If any one worked on this scenario suggest me the best approcah.
Our motto is to use single master db for all client instances.
Hi Sathish,
I don’t think that this is possible directly (in normal ways). I can suggest you following two ways to implement this scenario:
1. You can use formula field to query against another database. One of the advantage formula field is – it can participate in the hibernate queries.
2. Instead of defining domain property (that can interact with another database only), you define Long property that stores the id of that domain. And override the getter that fetch the domain instance from another database, like Burt answer in this (http://stackoverflow.com/questions/18768667/grails-gorm-domain-association-across-two-datasources) post.