Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access two databases in Grails

Tags:

grails

Grails makes it very easy to configure datasources for different environments (development, test, production) in its DataSources.groovy file, but there seems to be no facility for configuring multiple datasources in one environment. What to I do if I need to access several databases from the same Grails application?

like image 482
Ben Williams Avatar asked Sep 03 '08 01:09

Ben Williams


3 Answers

Connecting different databases in different domain classes is very easy in Grails 2.x.x.

for example

development {
    dataSource {//DEFAULT data source
      .
      .
    }
dataSource_admin { //Convention is dataSource_name
        url = "//db url"
        driverClassName = "oracle.jdbc.driver.OracleDriver" 
        username = "test"
        password = 'test123'
    }
dataSource_users {

    }
}

You can use any datasources in your domain classes by

class Role{
   static mapping = {
      datasource 'users'
   }
}

 class Product{
    static mapping = {
      datasource 'admin'
   }
 }

For more details look at this

like image 71
Sushanth CS Avatar answered Nov 06 '22 11:11

Sushanth CS


If using Grails 2.0 or higher, there is no need for the plugin, it is supported natively.

http://www.grails.org/doc/latest/guide/single.html#multipleDatasources

like image 20
Peter Avatar answered Nov 06 '22 11:11

Peter


There is now Grails plugin that enables the use of multiple datasources directly with Grails' GORM layer: http://burtbeckwith.com/blog/?p=70

like image 9
Ben Williams Avatar answered Nov 06 '22 10:11

Ben Williams