Spring, JDO and transactions on Google App Engine

Although I haven’t mentioned it in a while, I’m still continuing development on my Vaadin project. As with all new technology, sometimes some frustration is involved. Here’s an overview of how I’m currently using Spring’s transaction management and its JDO support. The setup seems easy enough, until you try to run it on the actual AppEngine. Turns out there are some details you need to take care of.

I use an approach similar to the one

Objectuser describes in his post. It really is a piece of cake, you just set up a LocalPersistenceManagerFactoryBean to point to the “transactions-optional” factory that the App Engine supplies:

<bean id="persistenceManagerFactory">
  <property name="persistenceManagerFactoryName" value="transactions-optional" />
</bean>

After that you can use everything that Spring supplies. For instance, I went for an annotations based transactions management instead of defining my own pointcuts as in the post mentioned above. The documentation on the Spring site gives a pretty good, although condensed, explanation of everything you need to know.

Locally, everything works just fine, but when I deployed the application, I ran into a few issues. Most importantly, using the TransactionAwarePersistenceManagerFactoryProxy resulted in a java.lang.NoClassDefFoundError: javax/naming/NamingException. Apparently, somewhere in the bowls of that code a catch block uses this exception. There are a few posts around the web that suggests rolling your own version of some of the Spring libraries. That’s something I really really want to avoid, I’d rather just get rid of Spring if that were the only solution.

There was a different solution though: Doing my own transaction management. Always closing persistence managers after loading and persisting entities. I centralized all my datastore operations, so that was fairly easy, but it still is too bad I can’t take the easy way out and let Spring handle it for me.

It is however another library I can remove from the dependencies, so once I get to optimizing the application that’s already a small win.

Another gotcha was when I first used an index (apart from the primary keys). The local server does not complain when you haven’t explicitly defined your indexes, it simply generates one on the fly. The real App Engine runtime however requires that you define the index before using it. It is possible to use an autogenerated file, but this one sometimes gets cleaned up, so you might suddenly loose your indices.

This is all well documented in the App Engine reference guide, so really, there’s no one to blame but me. Still, I have a feeling many developers will run into this issue, so I thought I’d mention it.