Search code examples
grailsgrails-ormdatasource

Grails plugin cannot access to multidatasources


I have a plugin (Let's call its plugin A) with option domain entities and one more plugin with obligatory entities (plugin B). Plugin B also has a definition of 2 datasources (default dataSource and dataSource_redshift). In my application originally I have dependency on plugin B and can use both datasources without any problems. However I have now add plugin A as dependecy to my application and entity with default datasource seem like working well, but in case I'm creating such entity:

class RedshiftEntity {

    static mapping = {
        datasource "redshift"
        table name: "table", schema: "public"
        id generator: 'assigned', column:'id'
        version false
    }

    ...
    fields
    ...
}

On load of application I get such error:

Caused by: org.grails.datastore.mapping.core.exceptions.ConfigurationException: DataSource not found for name [dataSource_redshift] in configuration. Please check your multiple data sources configuration and try again.
    at org.grails.orm.hibernate.HibernateDatastore.getDatastoreForConnection(HibernateDatastore.java:337)
    at org.grails.orm.hibernate.HibernateGormEnhancer.getStaticApi(HibernateGormEnhancer.groovy:47)
    at org.grails.datastore.gorm.GormEnhancer.registerEntity(GormEnhancer.groovy:139)
    at org.grails.datastore.gorm.GormEnhancer.<init>(GormEnhancer.groovy:122)
    at org.grails.orm.hibernate.HibernateGormEnhancer.<init>(HibernateGormEnhancer.groovy:41)
    at org.grails.orm.hibernate.HibernateDatastore.initialize(HibernateDatastore.java:414)
    at org.grails.orm.hibernate.HibernateDatastore.<init>(HibernateDatastore.java:177)
    at org.grails.orm.hibernate.HibernateDatastore.<init>(HibernateDatastore.java:188)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrConstructorNewInstance(ReflectiveInterceptor.java:1075)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
    ... 84 common frames omitted

In case I move this entity to plugin B - it works great with dataSource_redshift. Please, give an idea what am I missing?


Solution

  • I have found the root cause of such problem. Plugin B have special construction to enable or disable the second data source for apps by there name:

    switch(app_name) {
        ...
        case 'main_app':
            redshiftEnabled = true 
        ...
    }
    

    Redshift data source is enabled for my main app, but seems like data sources are not transparent for plugins as I expected. When plugin B with data sources are added as a dependency to the main app, other connected to the app plugins are not allowed to access the second data source. I need explicitly enumerate plugins by there personal names in plugin with data sources:

    switch(app_name) {
        ...
        case 'main_app':
            redshiftEnabled = true
        case 'plugin_A':
            redshiftEnabled = true 
        ...
    }
    

    I'd rather expect that after plugins are connected to the main app they access data sources with the name of the main app, but seems like it does not so.