Friday, January 31, 2014

Spring Data on GAE - Part 1 - Basic JPA

I would like to create an applicaton using AngularJS as front-end and Spring as back-end. The data access layer will be implemented by Spring Data JPA with DataNucleus JPA provider. The whole application will be running on Google App Engine.

There was debate over JPA vs JDO on app engine (see here). Personally I prefer to use JPA due to its widely adoption and also its support by Spring Data. However, we will see some design issues later due to the App Engine Datastore owned relationship feature.

1. Data model


Firstly I need to define a simple data model for this experiment. Here I created a POJO named Player to represent the players on the game server:

@Entity
public class Player {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String rank;

    public Player() {
    }

    public Player(String name, String rank) {
        super();
        this.name = name;
        this.rank = rank;
    }

    // setters and getters skipped
}

Due to the design of Datastore, we will soon see problem of using Long as the primary key. However, for simplicity we just use Long here temporarily.

2. JPA Configurations


As I use Spring MVC and Spring Data to implement the server side presentation and data layer, I add the following dependencies in build.gradle:

build.gradle
dependencies {
    compile "org.springframework:spring-webmvc:3.2.6.RELEASE"
    compile "org.springframework.data:spring-data-jpa:1.3.0.RELEASE"
    compile "com.fasterxml.jackson.core:jackson-databind:2.3.1"
    compile "org.datanucleus:datanucleus-enhancer:3.1.1"
    compile "org.codehaus.jackson:jackson-mapper-asl:1.9.9"
    compile "org.slf4j:slf4j-simple:1.7.5"
    ...
}

Note that the older spring-data-jpa 1.3.0 is used instead of the latest release because of a bug in DataNucleus (http://www.datanucleus.org/servlet/jira/browse/NUCJPA-250).

To specify DataNucleus as the JPA provider, the persistence unit is set as follows:

META-INF/persistence.xml
<persistence version="2.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/persistence" 
    xsi:schemalocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="jpa.unit">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
        <class>com.angularspring.sample.domain.Player</class>
        <exclude-unlisted-classes />
        <properties>
            <property name="datanucleus.ConnectionURL" value="appengine" />
            <property name="datanucleus.appengine.ignorableMetaDataBehavior" value="NONE" />
        </properties>
    </persistence-unit>
</persistence>

3. Spring Data Repository


We need something to actually interact with the database. Here I do not use native JPA API (i.e. EntityManager) for database operations, instead I use Spring Data JPA. Spring Data can greatly reduce boilerplate codes for DAO implementation. We just need to define a player repository interface:

package com.angularspring.sample.repository;
public interface PlayerRepository extends JpaRepository<Player, Long> {
}

To complete the JPA repository configuration, we need to specify the followings in a Spring configuration:

applicationContext.xml
<jpa:repositories base-package="com.angularspring.sample.repository" />

<tx:annotation-driven transaction-manager="transactionManager" />

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <constructor-arg ref="entityManagerFactory" />
</bean>

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="jpa.unit" />
    <property name="packagesToScan" value="com.angularspring.sample.domain" />
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" />
    </property>
</bean>

4. Spring MVC Restful Service


The Spring MVC is used to implement RESTful service. Firstly, the web.xml is setup to include the DispatcherServlet and the context loader (loads the Spring configuration in previous section):

web.xml
<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/service/*</url-pattern>
</servlet-mapping>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


And the following configuration for the dispatcher:

dispatcher-servlet.xml
<mvc:annotation-driven />
<context:component-scan base-package="com.angularspring.sample" />
<context:annotation-config /> 

5. Spring MVC Controller


Finally, we need to add a Spring MVC Controller to test the application from browser. A PlayerController class is configured to map the path "/service/players" to a service returning all players, and map path "/service/players/{id}" to return a particular player, both in JSON format. This object to JSON conversion is handled automatically by the jackson-mapper-asl library if we put that in classpath.

@Controller
@RequestMapping("/players")
public class PlayerController {
    private static Logger logger = LoggerFactory.getLogger(PlayerController.class);

    @Autowired
    PlayerRepository playerRepository;

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody List<player> getAllPlayers() {
        return playerRepository.findAll();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public @ResponseBody Player getPlayer(@PathVariable("id") Long id) {
        return playerRepository.findOne(id);
    }

    @RequestMapping(value = "/initDB", method = RequestMethod.GET)
    @Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED)
    public ResponseEntity<string> initDB() {
        List<player> players = new ArrayList<player>();
        players.add(new Player("Snoopy", "9p"));
        players.add(new Player("Wookstock", "9p"));
        players.add(new Player("Charlie", "1d"));
        players.add(new Player("Lucy", "4d"));
        players.add(new Player("Sally", "5d"));
        playerRepository.save(players);
        return new ResponseEntity<string>("5 players inserted into database", HttpStatus.OK);
    }
}

I have also added an initDB() method to initialize the datastore using HTTP request. The above will add 5 players into the database.

6. Test the application


We could test the application by using the Eclipse Google plugin. Start the google web application and try with the following in your browser (assume port is 8888). I used Firefox instead of IE because Firefox will output JSON directly.

    http://localhost:8888/service/players

It should return nothing, as expected, because we have no records in the database. So what we see is an empty array in JSON format (i.e. [ ]).

We can then initialize the database by:

    http://localhost:8888/service/players/initDB

Query the players again. It should give us something like the following:

[{"id":4573968371548160,"name":"Sally","rank":"5d"},{"id":4925812092436480,"name":"Snoopy","rank":"9p"},{"id":5488762045857792,"name":"Charlie","rank":"1d"},{"id":6051711999279104,"name":"Wookstock","rank":"9p"},{"id":6614661952700416,"name":"Lucy","rank":"4d"}]

7. Problem


It looks simple to use JPA with GAE.

However, when we test the application in browser, one would easily discover something unusual: not all entities are displayed immediately after the database initialization! It may display 2 records at the first time, 3 records at the second time, and finally get all 5 records displayed if we submit the HTTP request enough times. The reason is about the eventual consistency characteristic of Google App Engine. All those entities are unowned entities, so they are not belonging to the same entity group. In App Engine Datastore architecture, transactionality and data consistency only apply to entity group level.

For details please read the owned relationship of Datastore entities. Also note the unsupported features of JPA on Datastore (e.g. we cannot do many-to-many mapping) when you do the data modelling.

The handling of owned relationship and transactionality on GAE will be discussed in next part.

The source can be found at GitHub (tagged v0.1).