Announcement Announcement Module
Collapse
No announcement yet.
Overriding Grails Database Configuration. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Overriding Grails Database Configuration.

    In Grails if we want to use database we have to use Grails Datasource.groovy. We have almost 15 different envoirnments in which we deploy our applications. on top of this we have another requirement that we will create one war that can be deployed in any of evironments without needing to recreate/recompile the code.
    like we have dev, test, nightly, UAT, disaster recovery ...etc & then Production envoirnment where we deploy our application.

    Normally what we do is that we create our own envoirnment settings file and based on SITE location e.g. computer naming pattern we write our dataSources. So it will read dynamically at runtime where the code is executing. How can Envoirnment help us is this situation ? I am bit naive in Grails/Groovy.

  • #2
    I'm not quite sure what you mean by "we write our dataSources". You can probably do the same with Grails.

    Anyway, if you want to be able to use the same WAR file in different environments, then Grails environments won't really help. One common approach if you're using Tomcat is to add these lines to your grails-app/conf/Config.groovy file:

    Code:
    grails.config.locations = [ "file:./${appName}-config.groovy", "classpath:${appName}-config.groovy" ]
    So if your application has the name 'my-app', you can put a file called 'my-app-config.groovy' into Tomcat's 'lib' directory. This will override any settings in Config.groovy and DataSource.groovy. For example, I have this in a local 'site-config.groovy' file:

    Code:
    environments {
        development {
            dataSource.url = "jdbc:mysql://localhost/grails_org_new"
            dataSource.driverClassName = "com.mysql.jdbc.Driver"			
            dataSource.username = "root"
            dataSource.password = ""
            dataSource.jndiName = null
            dataSource.dbCreate = "create-drop"
        }
        ...
    }
    An alternative approach is to put code into DataSource.groovy that evaluates the connection settings at runtime, via system properties or some other mechansim:

    Code:
    environments {
        development {
            dataSource {
                dbCreate = "update" // one of 'create', 'create-drop','update'
                url = System.getProperty("JDBC_CONNECTION_STRING")
                driverClassName = System.getProperty("JDBC_DRIVER_CLASS")
                username = System.getProperty("JDBC_USERNAME")
                password = System.getProperty("JDBC_PASSWORD")
            }
        }
        ...
    }
    You don't have to use system properties. Almost any code would be valid.

    Comment


    • #3
      Originally posted by pledbrook View Post
      I'm not quite sure what you mean by "we write our dataSources". You can probably do the same with Grails..
      Sorry about confusing English. Ok so we have multiple Sites where we want to deploy our applications on tomcat. There applications are messaging framework talking to MQ. Now with GORM functionality we will have good impact on our productivity as we dont have to write lot of code. but on other hand we want to keep configurations to minimal and in each environment ship single war file. You proposed a good solution about overriding the sources from static place, which wont change.

      Thank you very much.

      Comment

      Working...
      X