Mcgyver83
Mcgyver83

Reputation: 81

DB data lost for each cloudfoundry deploy of grails application

I'm developing a grails 2.0.3 application using sts. I develop and before close sts I usually deploy my application on cloudFoundry. I'm using HSQLDB and this is DataSource.groovy:

dataSource {
    pooled = true
    driverClassName = "org.h2.Driver"
    username = "mcg"
    password = "mcg"
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = true
    cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
}
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "update" // one of 'create', 'create-drop','update'
            url = "jdbc:h2:file:qhDB"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:file:testDb"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:file:prodDb"
        }
    }
}

My problem is that each time I deploy my application to cloudfoundry the db becomes empty on the cloud.

Some suggestions?

Upvotes: 1

Views: 716

Answers (3)

Mcgyver83
Mcgyver83

Reputation: 81

I solve using MySQL service on cloudfoundy.

Upvotes: 1

Burt Beckwith
Burt Beckwith

Reputation: 75681

@kenota is correct, but there's the additional risk that the entire instance can crash and get rebuilt, so you would lose all filesystem files, even in /tmp. You're much better off using MySQL or PostgreSQL - both are trivial to use in CloudFoundry and will perform much better. In addition if you have enough traffic that you need multiple web server instances, you will share one database instead of multiple file-based databases that all have different data.

Upvotes: 2

kenota
kenota

Reputation: 5662

By doing this:

url = "jdbc:h2:file:prodDb"

You are asking H2 to use file to store data. But problem is, you are using relative path, so the file will be created in the current working directory of web application, which is usually unpacked web app root.

If you run it on tomcat, the file will be located at: /opt/tomcat7/webapps/app/prodDb If you will redeploy your application with deleting previous one, the database file will be deleted as well.

I think that is exactly what is happening on cloudfoundry.

You should define absolute path to store your database:

url = "jdbc:h2:file:/tmp/prodDb"

Upvotes: 1

Related Questions