Search code examples
grailsruntimedatasource

How to get data from another data base in runtime


I need an optimal way to change Database in run-time, whether from hibernate or from JNDI or another. I need to change the database when a user clicks on a specific button (Change dependencies on events).


Solution

  • If you are running Grails 1.3.x you can to use the Burt Beckwith's Datasources plugin, in Grails 2.x that feature has been added to core.

    Then you can to use a parameter to pass database connection name

    def database = params.database
    if(!database) database = "default"
    

    and to use Groovy dynamic method invocation to call it

    def zipCode = ZipCode.'${database}'.get(42)
    …
    zipCode.'${database}'.save()