2

I have tried asking this in another question, but I may have put too many details.

I am using Grails and multiple datasources. If you want to use services to declare the datasource, that does NOT work at all for me.

static datasource = "db1"

No matter what, this has not worked for me in Grails Services

Thank you for any help/suggestions.

== I am editing this to include my Datasource.groovy

Now, this works fine if I use the static mapping on the domain object for my second database. However, I want the service to decide what database must be written to, therefore I was hoping that the service datasource attribute works as in the documentation.

  1. If I declare that my domain object uses "ALL" datasources, the service will write to the default datasource, despite the fact that I set the

    static datasource = "db21"

  2. If I declare that my domain object uses "db1" datasource, the service will write to the db1 datasource

    1. If I declare 2,3...N datasources, it looks like the service will only write to the default datasource or the datasource I had declared in the domain groovy file. The documentation says I should be able to use services to choose the 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"
            }
        }

    }
}
Kyle
  • 157
  • 1
  • 6
  • Perhaps I was confusing on explaining my issues: I have mentioned this here 1. http://stackoverflow.com/questions/8159862/can-i-have-2-different-datasources-in-groovy-with-different-privileges/10885621#10885621 and here: 2. http://stackoverflow.com/questions/10857663/grails-2-multiple-dynamic-datasources-in-services – Kyle Jun 05 '12 at 20:19
  • I had hoped @BurtBeckwith would weigh in on this issues as it seems he is the authority in this are and by far the most experienced. Thank you in advance. – Kyle Jun 05 '12 at 20:21
  • Please show your DataSource.groovy! Also, could you describe what isn't working in your question and the version you are using? – Michael J. Lee Jun 06 '12 at 00:49
  • Hi Michael, I edited my question. I hope this is a little more clear. Thanks for your time. If you take a look at my first comment, I did explain in as much detail as I could in my original question there but it was very lengthy and it never got any attention. – Kyle Jun 06 '12 at 16:11
  • 1
    Are you trying to dynamically 'swap' out the dataSource of a domain class through a service? If so I'm not sure this is possible. – Michael J. Lee Jun 06 '12 at 18:32
  • That is what I am trying. The documentation says that I should be able to decide in the service what datasource I use. here: http://grails.org/doc/2.0.0.RC1/guide/conf.html#multipleDatasources it says: " Like Domain classes, by default Services use the default DataSource and PlatformTransactionManager. To configure a Service to use a different DataSource, use the static datasource property, for example: class DataService { static datasource = 'lookup' void someMethod(...) { … } " – Kyle Jun 06 '12 at 19:48
  • Right; I am familiar with the docs but, this is a little different. Lets hope Mr. Beckwith jumps in. – Michael J. Lee Jun 07 '12 at 00:24

1 Answers1

1

We have used multiple datasources in our Grails applications and have had success switching the datasource in the Service using the approach that you indicated above...

static datasource = "db1"

However, the difference is that in all of our domain objects we have defined which datasource the domain object belongs to. I am not sure if it is possible to do without defining the non-default datasource in the mapping.

static mapping = { datasource 'db1' }

In some cases, we will have 2 different domain classes that have the same name but point to different datasources. In order to keep this clean, we will put the 2 domain classes in different packages and the service that uses those domain classes/datasources in the same package.

for example...

//Domain Classes
package com.yourcompany
class Student {
    static mapping = {
       //if you don't indicate the datasource it will use the default                      
    }
}

package com.yourcompany.db1
class Student {
    static mapping = {
       datasource 'db1'                     
    }
}

//Services
package com.yourcompany
class DefaultDbService {
   def getStudents() {
      //This will query the default datasource           
      Student.findAll()
   }
}

package com.yourcompany.db1
class Db1Service {
   static datasource = "db1"

   def getStudents() {
       //This will query the 'db1' datasource           
       Student.findAll()
   }
}

You might try this approach and see if it gets you the result that you are looking for.

EricD
  • 67
  • 1
  • 6