0

I have the following domain class that I need to map to a specific datasource (oracle), while all other my domain classes are mapped to the default dataSource (mysql, which works perfectly).

My editor is GGTS and as you can notice "datasource" inside mapping appears underlined, as if it were not a valid property inside mapping: GGTS screenshot

I also built a simple controller:

class AccountWfmController {
    def index() {
        render AccountWfm.list() as JSON
    }
}

when trying to call the controller method index, I get the following exception:

java.lang.IllegalStateException

Method on class [mypackage.AccountWfm] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.

Finally, my datasource (note: in my /lib folder I have ojdbc6.jar):

dataSourceWFM {
    pooled = true
    dialect = org.hibernate.dialect.Oracle10gDialect
    driverClassName = 'oracle.jdbc.OracleDriver'
    username = "username"
    password = "password"
    url = "jdbc:oracle:thin:@192.168.1.1:1521:SID"
    dbCreate = '' //none, I have readonly access
}

any hints on what is happening here (Grails 2.4.3)?

Thanks

Danilo
  • 2,676
  • 7
  • 32
  • 36

1 Answers1

4

Take a look at the Grails documentation on Multiple datasources. Pay close attention to how the name of the datasource is arrived at. You will notice that the naming convention, since Grails is based on conventions, is: dataSource_extraNameHere where your mapping will use extraNameHere.

So for your example:

dataSource_wfm {
    pooled = true
    dialect = org.hibernate.dialect.Oracle10gDialect
    driverClassName = 'oracle.jdbc.OracleDriver'
    username = "username"
    password = "password"
    url = "jdbc:oracle:thin:@192.168.1.1:1521:SID"
    dbCreate = '' //none, I have readonly access
}

and in your domain class your mapping should be like:

static mapping = {
  datasource = 'wfm'
}

Hope this helps shed some light on how multiple datasources function within Grails. The documentation is a good source of information as well.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • Thank you Joshua for your answer. The issue is indeed related to the naming convention "datasource_extraName", which I indeed missed while reading the documentation about multiple sources. – Danilo Feb 03 '15 at 10:09
  • You're more than welcome. I'm glad it was just an oversight. There is a lot to take in when learning Grails but we learn through our mistakes. I just remember this because of how excited I was when multiple datasource support was added into Grails. – Joshua Moore Feb 03 '15 at 10:19