Michal_Szulc
Michal_Szulc

Reputation: 4177

Binding bean with dataSource

In my ExampleService I tried to get bean with dataSource_other (I have to work with several db):

class ExampleService {
    def grailsApplication
    def connectAndCheck(){
        def sourceDatabase = grailsApplication.mainContext.getBean('dataSource_other')
    }
}

In my DataSource.groovy:

environments {
    production {
        dataSource_information_schema {
            ipDbServer = "1.2.3.4"
            db = "information_schema"
            username = "user"
            password = 'pass'
            pooled = true
            driverClassName = "com.mysql.jdbc.Driver"
            dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
            url = "jdbc:mysql://${ipDbServer}/${db}?useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true"
            readOnly = true
        }

        dataSource_other {
            ipDbServer = "1.2.3.5"
            db = "other"
            username = "user2"
            password = 'pass2'
            pooled = true
            driverClassName = "com.mysql.jdbc.Driver"
            dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
            url = "jdbc:mysql://${ipDbServer}/${db}?useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true"
        }

Unfortunatelly I've got an error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource_other' is defined

It works fine in dev environment where I read dataSource properties from Config file, why does it not in production? How to make it properly?

Upvotes: 0

Views: 118

Answers (1)

Shashank Agrawal
Shashank Agrawal

Reputation: 25797

You can use like this:

import org.grails.datastore.mapping.core.Datastore

grailsApplication.mainContext.getBeansOfType(Datastore).values().each { d ->
    println d
}

See the print statement and find out your bean for the datasource.

Upvotes: 1

Related Questions