Grails 3.x multiple datasources - grails

Back in Grails 2.x world I could create multiple dataSources in an environment:
development {
dataSource {
...
}
dataSource_new {
...
}
}
and reference them in the controller:
def db = new SQL(dataSource_new)
and everything worked awesome. In Grails 3.x, everything is not awesome:
development:
dataSource:
...
dataSource_new:
...
and calling
def db = new SQL(dataSource_new)
throws:
Ambiguous method overloading for method groovy.sql.Sql#
Anyone have success with this (or can point out what's changed that I've missed)?
Tried mapping in domain with no luck:
class abc {
String ...
static mapping = {
datasource: ['DEFAULT', 'dataSource_new']
}
throws:
Ambiguous method overloading for method groovy.sql.Sql#<init>. Cannot resolve which method to invoke for [null] due to overlapping prototypes between: [interface java.sql.Connection] [interface javax.sql.DataSource]

I work with multiply datasources in Grails 3.x like following:
assuming you have configuration:
development {
dataSources {
dataSource {
url = 'your_url'
password = 'psw'
...
}
second {
url = 'you_url_2'
password = 'psw2'
...
}
}
So in a service you will have mapped data source like this:
dataSource_second
And to create Sql instance you will need to do the following:
def sql = new Sql(dataSource_second)
It works in Grails 3.0.11.
Hope it will help.

In official docs you can find an example for injecting datasource into service:
https://grails.github.io/grails-doc/latest/guide/single.html#multipleDatasources
class DataService {
static datasource = 'lookup'
void someMethod(...) {
//…
}
}
or into command:
http://grails.github.io/grails-doc/latest/guide/upgrading.html
import grails.dev.commands.*
import javax.sql.*
import groovy.sql.*
import org.springframework.beans.factory.annotation.*
class RunQueryCommand implements ApplicationCommand {
#Autowired
DataSource dataSource
boolean handle(ExecutionContext ctx) {
def sql = new Sql(dataSource)
println sql.executeQuery("select * from foo")
return true
}
}
Moreover look at this thread and quoted examples: https://github.com/grails/grails-core/issues/701

Related

grails4 migration Traits lazy load issue - HHH000142: Bytecode enhancement failed

I have migrated my application from Grails-3.x to Grails-4.1.1
Most of my Domain classes implemented the following Traits class (DynamicProperties), which has an implementation of GormEntity for some reason - to override the propertyMissing method.
trait DynamicProperties<D> implements GormEntity<D> {
def dynamic = [:]
def propertyMissing(String name, value) {
if (!propertyIsDatasource(name)) {
dynamic[name] = value
}
}
def propertyMissing(String name) {
if (propertyIsDatasource(name)) {
super.propertyMissing(name)
} else {
dynamic[name]
}
}
boolean propertyIsDatasource(String name) {
false
}
}
The above trait has been implemented by many domain classes like this
class Customer implements DynamicProperties<Customer> {
String customerCode
String customerName
Address address
....
}
Now, when I run my application, It is throwing the following exception
HHH000142: Bytecode enhancement failed: com.apps.billing.Customer.
Caused by: java.lang.NullPointerException
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:37)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:75)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
at **com.apps.billing.common.DynamicProperties$Trait$Helper.$init$**(DynamicProperties.groovy:7)
It used to work fine with Grails3.x
I had a similar problem when migrating a project to Grails 4.0.13.
I tracked the trigger of this problem down to having default values for member variables in a trait class. I see you have the same with the property dynamic being initialized with the empty map.
I recommend lazy initializing it in one of your propertyMissing methods.

Grails - Bootstrap - null pointers instead of services

I'd like to use data generator at init in my application. It works fine when create objects using .save() method, but it doesn't work when I want to use dedicated services, because of null pointers instead of injected services. That's my code:
I have defined DataGenerator bean inside conf/spring
beans = {
dataGenerator(DataGenerator)
}
My Bootstrap.groovy looks like:
class BootStrap {
def dataGenerator
def init = { servletContext ->
dataGenerator.generateData()
}
}
In `DataGenerator' I have:
class DataGenerator{
BookService bookService
def generateData() {
log.info("Generating books")
createBooks()
}
def createBooks(){
(1..40).each() {
CreateBookCommand command = new CreateBookCommand()
/* some command populate code*/
bookService.create(command);
}
}
}
The problem is, that I cannot invoke create() method, because bookService is always null
BookService is simple grails service with some dependencies, of course placed in grails-app/services
class BookService {
UserService userService
SpringSecurityService springSecurityService
def create(CreateBookCommand command){
Book book = new Book()
command.bindTo(book)
book.save(flush:true, failOnError:true)
}
/*some other methods*/
}
Could you tell me how to fix it?
Try this in resources.groovy
beans = {
dataGenerator(DataGenerator) { bean ->
bean.autowire = 'byName'
}
}
I assume DataGenrator being a class outside the grails artifact (that is: placed in src/groovy), you can refer the already available service class in the context as:
beans = {
dataGenerator(DataGenerator){
bookService = ref('bookService')
}
}
or try autowiring byName as mentioned by #sudhir.

Set datasource values during run time

So essentially I'm trying to get my project up and running on AppFog. The datasource information is stored in an enviornment variable, which is essentially JSON. My goal is to take this data and set my datasource config from it.
Here is what I have tried:
Code to set the datasource config which is a method in a POGO. The POGO is instantiated and the method called at the beginning of DataSource.groovy:
import appfog.ParseDataSource
new ParseDataSource().setConfig()
dataSource {
...
}
class ParseDataSource {
void setConfig() {
String env = java.lang.System.getenv("VCAP_SERVICES")
if (env) {
def config = JSON.parse(env)
config = config["mysql-5.1"][0].credentials
grailsApplication.config.environments.production.dataSource.username = config.username
grailsApplication.config.environments.production.dataSource.password = config.password
grailsApplication.config.environments.production.dataSource.url = "jdbc:mysql://" + config.host + ":" + config.port + "/" + config.name
}
}
}
The problem is that grailsApplication is always null. I've tried registering a spring bean in resources.groovy:
beans = {
parseDataSource(appfog.ParseDataSource) {
grailsApplication = ref('grailsApplication')
}
}
class ParseDataSource {
def grailsAPplication
...
}
I've also tried getting it via Holders:
GrailsApplication grailsApplication = Holders.grailsApplication
Either way it is null so I'm not doing something right. Any ideas?
I think you are making this overly complex. Overwriting the grails config object while still in the process of building it would cause an order of operations issue that would make the code very fragile.
Simply setting the values directly seems more straightforward:
Datasource.groovy:
def configJson = JSON.parse(java.lang.System.getenv("VCAP_SERVICES"))
def mysqlConfig = configJson["mysql-5.1"][0].credentials
dataSource = {
production = {
username = mysqlConfig.username
// etc.
}
}
If you wanted to keep parsing in its own class for clarity's sake, make the values properties and read them in the dataSource block rather than trying to put them in the grails config object:
config parsing:
class EnvironmentConfigParser {
String username
String password
String url
EnvironmentConfigParser() {
def configJson = JSON.parse(java.lang.System.getenv("VCAP_SERVICES"))
def mysqlConfig = configJson["mysql-5.1"][0].credentials
username = mysqlConfig.username
password = mysqlConfig.password
url = "jdbc:mysql://${mysqlConfig.host}:${mysqlConfig.port}/${mysqlConfig.name}"
}
}
in Datasource.groovy:
def parser = new EnvironmentConfigParser()
dataSource = {
production = {
username = parser.username
// etc
}
}
You should be able to access grailsApplication the way you have injected in resources.groovy provided you are injecting the bean parseDataSource somewhere in your application in any artefact.
In your special case you need the bean to be available in datasource.groovy. You were instantiating the POGO which will not help you injecting grailsApplication to the POGO. On the other hand, you cannot actually inject the POGO to datasource.groovy like
def parseDataSource
because it(datasource) is a config object during bootstrap.
The best way remains will be to metaClass the pogo at BootStrap and make grailsApplication available to it. Burt has shown it here exactly that way.
I was also thinking whether BeanPostProcessor can be useful in this case but I am not sure whether config per environment will be achieved. But you can give it a try if it helps in achieving your business need. It generally goes like:
//src/groovy
import org.springframework.beans.factory.config.BeanPostProcessor
class DatasourcePostProcessor implements BeanPostProcessor{
def parseDataSource
#Override
Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean
}
#Override
Object postProcessAfterInitialization(Object bean, String beanName) {
if(beanName == 'dataSource') {
//Set values to dataSource bean as required
parseDataSource.setConfig(bean)
}
return bean
}
}
//resources.groovy
parseDataSource(ParseDataSource){
grailsApplication = ref('grailsApplication')
}
datasourcePostProcessor(DatasourcePostProcessor){
parseDataSource = ref('parseDataSource')
}

How to use Spock to test a Grails service method that uses a static method from a Domain Class?

So, I am trying to test the following Grails service implementation:
class BookService {
def searchBooks(String search) {
if (!search) {
return []
}
Book.searchBooks(search)
}
}
It uses a method defined in the Book domain class:
class Book {
String title
static def searchBooks(String search) {
Book.findByTitleLike(search)
}
}
So, this is the test code I've been trying:
#TestFor(BookService)
#Mock(Book)
class BookServiceSpec extends Specification {
void "should search books"() {
setup:
new Book(title: 'The Stand').save()
new Book(title: 'Under the Dome').save()
new Book(title: 'Bag of Bones').save()
when:
service.searchBooks('ones')
then:
1 * Book.searchBooks()
}
}
But, it always fails with:
Too few invocations for:
1 * Book.searchBooks() (0 invocations)
I've debugged the code running in an IDE and the method seems to be indeed executed, but Spock does not register the execution.
What am I missing?
Is there something else I have to do to mock the domain class method?
Here is a sample project with the code.

Grails .findBy doesn't work with redis

I have a very simple test case I am using to try to understand redis. I did install-plugin redis-gorm.
Domain Object:
class BenchGroup {
String groupName
/*static mapWith = "redis"
static mapping = {
groupName(index:true)
}*/
static constraints = {
}
}
Bootstrap Code:
def everyoneGroup = new BenchGroup(groupName:'everyoneGroup')
everyoneGroup.save()
if(everyoneGroup.hasErrors()){
println everyoneGroup.errors
}
println everyoneGroup
def dammit = BenchGroup.findByGroupName('everyoneGroup')
println dammit
When I leave the redis map line commented it uses HSQL and outputs this:
stupidbenchmarks.BenchGroup : 2
stupidbenchmarks.BenchGroup : 2
When I switch to redis it does this:
stupidbenchmarks.BenchGroup : 2
null
i.e. .findBy doesn't work.
Hibernate flushes before doing queries (in this case findByGroupName) but the NoSQL GORM Datastore implementations don't (yet) so I assume you just need a flush to push the saved instance to the datastore so the query picks it up:
everyoneGroup.save(flush: true)

Resources