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?