0

Sometimes we have one application which uses multiple database schemata.

E.g. There is a table company1.someTable which looks exactly like company2.someTable. But some users have access to company1., others to company2. oder both.

Is there an easy way to tell grails to work with a database like this and let the user select the schema?

1 Answer 1

1

You could try Datasources plugin.

http://www.grails.org/plugin/datasources

I have managed to connect two different mysql databases (postgres should be the same).

In grails project run command:

grails install-plugin datasources

Create file conf/Datasources.conf, which will hold the second schema (the default is still in Dataseource.conf)

for example:

datasources = {

datasource(name: 'wadmin') {
    driverClassName('com.mysql.jdbc.Driver')
    dbCreate("update")
    url("jdbc:mysql://localhost/wadmin-test")
    username("xx")
    password("xx")
    // here you will write list of classes in particular schema
    domainClasses([cz.webarchiv.wadmin.Curator, cz.webarchiv.wadmin.Publisher])
    dialect(org.hibernate.dialect.MySQL5InnoDBDialect)
    pooled(true)
    environments(['development'])
}

}

Mind the small "s" letter in datasources.

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the hint, but I think this plugin does not exactli cover my needs. I would like to have one application for multiple schemata/databases with the same tables where the user can select which one he would like to use.
Sorry, I misunderstood the question. But still you need that kind of functionality, but based on some parameter rather than class name. From understanding of the source I would look into persistenceInterceptor bean. Other approach (more messy) would be creating superclass, which will be inherited to subclasses. E.g. package.company1.Class & package.company2.Class extends package.SuperClass. This way you could use this plugin. But I don't like it.. it was just thought :)
Thank you anyway. The following blog article is probably closer to me needs: leebutts.com/2008/07/switchable-grails-datasource.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.