Last Updated: February 25, 2016
·
2.696K
· akbertram

Hibernate on Google AppEngine

We're in the process of migrating ActivityInfo.org to Google AppEngine and their MySQL-based Cloud SQL service.

The experience has been quiet straightforward, but we've had to a do a bit of tuning to get Hibernate working well on AppEngine.

First, you have to completely disable connection pooling. CloudSQL instances pop up and down so you can't hold on to a Connection beyond the duration of a request. This can be done through configuration:

hibernate.connection.pool_size = 0

The second issue is startup time. By default, Hibernate 3.6 (the version we're using) will scan your classpath looking for classes annotated with @Entity. This isn't a big deal when you bring up new instances once a day or so, but AppEngine instances pop up and down quite frequently.

Hibernate was taking 30+ seconds to initialize, which means that many warm up requests-- the first requests from the user to a new instance -- were timing out.

We were able to work around this by providing the list of classes explicitly using the Ejb3Configuration API:

Ejb3Configuration config = new Ejb3Configuration();
config.setProperty("hibernate.connection.driver_class",
  "com.google.appengine.api.rdbms.AppEngineDriver");
config.setProperty("hibernate.connection.url",
  "jdbc:google:rdbms://domain.com:project:instance/db");
config.setProperty("hibernate.connection.pool_size", "0");
config.addAnnotatedClass(MyEntity.class);
config.addAnnotatedClass(MyEntity2.class);
return config.buildEntityManagerFactory();

We had been using persistence.xml for configuration, but I wasn't able to find away to disable the class path scan without resorting to the Ejb3Configuration API.

3 Responses
Add your response

I am getting the following error when explicitly using the Ejb3Configuration API:

Class org.hibernate.cfg.Configuration can not access a member of class org.hibernate.cfg.beanvalidation.TypeSafeActivator with modifiers "public static"

Did you experience anything similar?

over 1 year ago ·

Hmm, no I didn't but it sounds like a version mismatch -- does your validator library match your version of hibernate?

over 1 year ago ·
over 1 year ago ·