Grails multiple DBs - use services to select the datasource does not work -


i have tried asking in question, may have put many details.

i using grails , multiple datasources. if want use services declare datasource, not work @ me.

static datasource = "db1" 

no matter what, has not worked me in grails services

thank help/suggestions.

== editing include datasource.groovy

now, works fine if use static mapping on domain object second database. however, want service decide database must written to, therefore hoping service datasource attribute works in documentation.

  1. if declare domain object uses "all" datasources, service write default datasource, despite fact set

    static datasource = "db21"

  2. if declare domain object uses "db1" datasource, service write db1 datasource

    1. if declare 2,3...n datasources, looks service write default datasource or datasource had declared in domain groovy file. documentation says should able use services choose datasource.

=============== edit ==============

datasource {     pooled = true     driverclassname = "org.h2.driver"     username = "sa"     password = "" }  hibernate {     cache.use_second_level_cache = true     cache.use_query_cache = true     cache.region.factory_class = 'net.sf.ehcache.hibernate.ehcacheregionfactory' } // environment specific settings environments {     development {         datasource {             dbcreate = "validate"             url = "jdbc:h2:devdb;mvcc=true"         }     datasource_db1 {         dbcreate = "validate"         url = "jdbc:h2:dev1db;mvcc=true"         pooled = true         driverclassname = "org.h2.driver"         username = "sa"         password = ""     }     }     test {         datasource {             dbcreate = "update"             url = "jdbc:h2:mem:testdb;mvcc=true"         }     }     production {         datasource {             dbcreate = "update"             url = "jdbc:h2:proddb;mvcc=true"             pooled = true             properties {                maxactive = -1                minevictableidletimemillis=1800000                timebetweenevictionrunsmillis=1800000                numtestsperevictionrun=3                testonborrow=true                testwhileidle=true                testonreturn=true                validationquery="select 1"             }         }      } } 

we have used multiple datasources in our grails applications , have had success switching datasource in service using approach indicated above...

static datasource = "db1" 

however, difference in of our domain objects have defined datasource domain object belongs to. not sure if possible without defining non-default datasource in mapping.

static mapping = { datasource 'db1' } 

in cases, have 2 different domain classes have same name point different datasources. in order keep clean, put 2 domain classes in different packages , service uses domain classes/datasources in same package.

for example...

//domain classes package com.yourcompany class student {     static mapping = {        //if don't indicate datasource use default                           } }  package com.yourcompany.db1 class student {     static mapping = {        datasource 'db1'                          } }  //services package com.yourcompany class defaultdbservice {    def getstudents() {       //this query default datasource                  student.findall()    } }  package com.yourcompany.db1 class db1service {    static datasource = "db1"     def getstudents() {        //this query 'db1' datasource                   student.findall()    } } 

you might try approach , see if gets result looking for.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -