I have a Grails project in which the development environment uses an in-memory H2 database:
dataSource {
dbCreate = "create"
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
I can run the app (using run-app) and put some stuff in the database (eg. using a scaffolded controller). Now, if I make a change to a source file, Grails detects the change, recompiles the class, and... clears the database!
I tried to use file
instead of mem
in the JDBC URL, but the behavior is the same. The only way I have found to avoid this is to set dbCreate
to "update"
instead of "create"
, but this is not what I want either, because I load a fixture in the application’s BootStrap
, so when the application is restarted I end up with multiple copies of my fixture.
So the question is: How can I prevent Grails from recreating the database when it recompiles a class — or, alternatively, to reload my fixture whenever it clears the database?
There is a similar "problem" here.
Grails resets the database whenever you make changes to a domain class.
Normally your domain classes shouldn't change too often, so this is not a big problem. Just put logic in controllers and services as you are meant to be, following the MVC pattern. Then try to do all pending domain class changes in one go.