gabe
gabe

Reputation: 1129

In grails 2 when using multiple datasources, can I specify which datasource a criteria should use?

In my grails 2 application I have multiple datasources configured in Datasources.groovy. My domain class mappings specify that all datasources should be used

class Book {
    static mapping = { datasource 'ALL' }

For basic Gorm calls I am able to specify which datasource to use.

Book.lookup.save() correctly uses the 'lookup' datasource

Is there a way to specify which datasource a criteria should use?

I have tried the following 4 solutions without any luck

  1. def c = Book.lookup.createCriteria(); c.list{...

    The call to .list throws: java.sql.SQLException: Connection is closed

  2. def c = Book.createCriteria(); c.lookup.list{...
    no such method

  3. Injecting the datasource

    def dataSource_lookup

    ...

    Book.createCriteria(dataSource_lookup)
    no such method

  4. Specifying that the whole service where the criteria is located should use a specific datasource doesn't seem to be working for the criteria nor for the basic save() call. I'm running grails 2.1.0.

    static datasource = 'lookup'

Upvotes: 2

Views: 1091

Answers (1)

gabe
gabe

Reputation: 1129

Not a great solution, however it does work if you access the datasource beforehand (presumably within the same hibernate session).

Book.lookup.read(1)
def c = Book.lookup.createCriteria()
c.list{...

Upvotes: 1

Related Questions