Search code examples
oracle-databasegrailsgrails-ormggts

Domain class mapping: datasource not working


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


Solution

  • 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.