2

We created two different domain objects in Grails and tried accessing from two different schema.

Method 1:

For example:

Student.groovy

class Students {
     String id
     String name
     String address 

   Static mapping = {
     schema: 'student_details' 
   } 
 }

Customer.groovy

class Customer {
    String firstName
    String lastName 
    String address  

   Static mapping = {
      schema: 'customer_details'
    }    
 }

application.yml

 environments:
    development:
       dataSource:
          dbCreate: update
          url: jdbc:mysql://localhost:3306/

If I provide a default schema in url connection string, it always referring to that default irrespective of schema defined in domain class and throw exception, no table found. If I remove default schema from url connection string, I am getting "No database selected" error in logs.

Method 2:

We tried to configure each schema with multiple data sources option in application.yml as follows:

dataSource:
    pooled: true
    dbCreate: update
    url: jdbc:mysql://localhost:3306/sample_grails
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    username: root
    password: ''
secondary:
    pooled: true
    dbCreate: update
    url: jdbc:mysql://localhost:3306/grails_mapping
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    username: root
    password: ''

Used the domain class as Customer.groovy

class Customer {
    String firstName
    String lastName 
    String address  

   Static mapping = {
      datasource 'secondary'
   }     
 }

I am getting an error

Caused by: org.grails.datastore.mapping.core.exceptions.ConfigurationException: DataSource not found for name [secondary] in configuration. Please check your multiple data sources configuration and try again.

We referred the following links for multiple schema access:

https://objectpartners.com/2016/03/09/using-secondary-datasources-in-grails-3/

Creating a Domain Class with schema in Grails

Can anyone suggest a solution to this?

Sinthu
  • 21
  • 2

2 Answers2

1

Your method 2 is almost there, I think what you are missing is- you need to define "secondary" under "datasources" - see this - http://docs.grails.org/latest/guide/conf.html#multipleDatasources

Alok Sinha
  • 171
  • 1
  • 3
0

In your application.yml file, use datasources: below your default datasource this way:

dataSource:
   pooled: true
   dialect: org.hibernate.dialect.MySQL5InnoDBDialect
   driverClassName: com.mysql.jdbc.Driver
   dbCreate: update
   url: jdbc:mysql://localhost:3306/default_schema
   username: root
   password: ''
datasources:
  source1:
    dialect: org.hibernate.dialect.MySQLInnoDBDialect
    driverClassName: com.mysql.jdbc.Driver
    username: root
    password: ''
    url: mysql://localhost:3306/source1
    dbCreate: update

Now, your Customer class should look like

class Customer {
String firstName
String lastName 
String address
Static mapping = {
  datasource 'source1'
}     
}

For furthermore details, you can see Multiple Datasources In Grails,official doc

BenHuman
  • 175
  • 13