About software development for the enterprise. Focus on Java EE and more general Java platforms.
You'll read a lot about Conferences, Java User Groups, Java EE, Integration, AS7, WildFly, EAP and other technologies that hit my road.

Friday, July 31, 2009

Spring 3.0.0.M3 on Google Appengine with JPA

09:10 Friday, July 31, 2009 Posted by Markus Eisele
, , , ,
I am working on a private project these days. It should give myfear.com and myfear.de a new meaning. Therefore I am trying out Google's Appengine.
Beeing forced to more or less full blown JEE5 in my dayly work, I wanted to try out something new. Having worked with older Spring versions some time ago, I feelt it was time to give this another shot. And so I ended up, trying Spring's newest milestone release on the GAE.

At the end of the day, I solved many problems on the road and everything worked fine. Here is a brief summary of what I had to change/find out:

1) GAE does not support full blown JEE. Therefore you have to be very sensible for using the right combination of technologies. If you try to use spring completely you will fail. The following list of jar files did the job for me:

2) Setting up the persistance.xml is straight forward but remember to rename the persistance-unit in jdoconfig.xml. Both should have different names!

<persistence-unit name="transactions-optional">
<property name="datanucleus.NontransactionalRead" value="true" />
<property name="datanucleus.NontransactionalWrite" value="true" />
<property name="datanucleus.ConnectionURL" value="appengine" />

3) Defining the entity manager in dispatcher-servlet.xml

<bean id="entityManagerFactory"
<property name="persistenceUnitName" value="transactions-optional" />
<bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />

Don't forget to add the following two, if you want to use JPA @Annotations in your Entities and Spring @Annotations in your DAOs and Services.

<tx:annotation-driven />

class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

4) Injecting the EntityManager in your @Repository Daos:

private EntityManager entityManager;

public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;

5) If you try to use the injected entityManager without any transaction, then you would most likely get an NucleusUserException: Object Manager has been closed. You can prevent this, using @Service and @Transactional in your service layer.

6)Nearly the same could happen, if you do not call queryResult.size() on your query.getResultList(). The error was reported several times. There seems to exist only this "workaround".

7) Using EL Expressions in your JSP forces you to use a little addon to the jsp page definition:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>

8) If you would like to use the <fmt:formatDate jstl tags, you have to enable session management in your appengine-web.xml. <sessions-enabled>true</sessions-enabled>

9) I came across several problems using a JRE with eclipse and GAE local server. You should always use a JDK!

All for now ... more to come :)