Grails multiple datasource, not working when trying to save a new Instance into selected datasource

I'm having this issue with a simple configuration when working with multiple datasources and I'm trying to save a new instance into a specific datasource

my datasources:

dataSources:
    dataSource:
        pooled: true
        jmxExport: true
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        driverClassName: com.mysql.jdbc.Driver
        username: root
        password: Choice2016
    dominio1:
        pooled: true
        jmxExport: true
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        driverClassName: com.mysql.jdbc.Driver
        username: root
        password: Choice2016
    dominio2:
        pooled: true
        jmxExport: true
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        driverClassName: com.mysql.jdbc.Driver
        username: root
        password: Choice2016

my domain

class Nxtt_reports {

    Boolean favorite

    static hasMany = [nxtt_report_histories: Nxtt_report_history, nxtt_user_reports: Nxtt_user_reports, nxtt_report_snapshots: Nxtt_report_snapshot]

    static constraints = {

    }

    static mapping = {
        datasource 'ALL'
    }

}

when I do this,

    def nxtt = Nxtt_reports.class
    println(nxtt.dominio1.list())

I can list data in the domain I want, but if I do this

    def nxtt = Nxtt_reports.class.newInstance()
    nxtt.favorite = 0
    nxtt.dominio2.save()

I'm getting this

No such property: dominio2 for class: nexttreport.server.Nxtt_reports. Stacktrace follows: groovy.lang.MissingPropertyException: No such property: dominio2 for class: nexttreport.server.Nxtt_reports

using:

| Grails Version: 3.1.1 | Groovy Version: 2.4.5 | JVM Version: 1.8.0_65

edit

environments:
    development:
        dataSources:
            dataSource:
                dbCreate: update
                url: jdbc:mysql://192.168.1.24:3306/nexttreport?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
            dominio1:
                dbCreate: update
                url: jdbc:mysql://192.168.1.24:3306/dominio1?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
            dominio2:
                dbCreate: update
                url: jdbc:mysql://192.168.1.24:3306/dominio2?useUnicode=yes&characterEncoding=UTF-8&useSSL=false

Answers


I have a bit of trouble reading a and understanding your code, because you are not following normal Java/Groovy conventions.

Firstly I would recommend using normal CamelCase and no underscores in your class names, so use NxxtReports.

Secondly, why not do NxxtReports.domino1.list() ? list() is a static method on the NxxtReports, so call it like that.

Thirdly and I think most importantly, use the normal Java/Groovy way of instantiating an Object, so do

def nxxt = new NxxtReport()
nxxt.favorite = 0
nxxt.domino2.save()

I suspect that the extra Grails methods on a DomainClass instance are not correctly added by calling newInstance() on the class variable.

I don't have a real computer at hand (on mobile now) so I can't check my code, but I think using normal Java/Groovy conventions will help a lot.


Need Your Help

Build my library in JavaScript/Dojo

javascript dojo

I will appropriate your help in creating my library in JavaScript and using Dojo.

Is there a way to turn off Windows Display Driver Model (WDDM) in Windows 7/8?

windows cuda operating-system

There are so many headaches brought with Windows Display Driver Model (WDDM), the latest being the weird 2G memory allocation bug on GPU.