How to make grails save database changes instead of dropping whenever restarted

Recently, I just started learning Grails and I realize that whenever I restart the grails application, any changes made to the database is not there anymore. After a bit of searching, I found out that we can change this behaviour in ./grails-app/conf/DataSource.groovy.

Below is the default configuration used for development environment:

development {
  dataSource {
    dbCreate = "create-drop" //one of 'create', 'create-drop', 'update', 'validate'
    url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
    }
}

The forgetful behaviour can be changed to persistent by changing the "create-drop" choice to "update". Besides that we need to change the url too, taking away the ':mem' from the line, so that the database doesn’t only live in the memory, but it will be saved into a file instead.

Changed configuration:

development {
  dataSource {
    dbCreate = "update" //one of 'create', 'create-drop', 'update', 'validate'
    url = "jdbc:h2:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
    }
}

We can also make use of ./grails-app/conf/BootStrap.groovy to populate some data into the database every time we start up the grails application. If we use this with the "create-drop" choice, we will have a clean set of data everytime we start the grails application.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s