TomEE and DataNucleus JPA

Apache TomEE ships with OpenJPA/EclipseLink as the default JPA provider (depending on version of TomEE), however any valid JPA provider can be used.

The basic steps are:

  • Add the DataNucleus jars to <tomee-home>/lib/
  • Configure the web-app or the server to use DataNucleus.

Webapp Configuration

Any web-app can specify the JPA provider it would like to use via the persistence.xml file, which can be at any of the following locations in a webapp

  • WEB-INF/persistence.xml of the .war file
  • META-INF/persistence.xml in any jar located in WEB-INF/lib/

A single web-app may have many persistence.xml files and each may use whichever JPA provider it needs. The following is an example of a fairly common persistence.xml for DataNucleus

<persistence version="1.0"
       xmlns="http://java.sun.com/xml/ns/persistence"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
       http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="movie-unit">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
        <jta-data-source>movieDatabase</jta-data-source>
        <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>

        <properties>
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
        </properties>
    </persistence-unit>
</persistence>

Note that you may have to set the persistence property datanucleus.jtaLocator and datanucleus.jtaJndiLocation to find your JNDI data sources.

Server Configuration

The default JPA provider can be changed at the server level to favour DataNucleus over OpenJPA/EclipseLink. Using the <tomee-home>/conf/system.properties file or any other valid means of setting java.lang.System.getProperties(), the following standard properties can set the default for any persistence.xml file.

javax.persistence.provider
javax.persistence.transactionType
javax.persistence.jtaDataSource
javax.persistence.nonJtaDataSource

So, for example, DataNucleus can become the default provider via setting

CATALINA_OPTS=-Djavax.persistence.provider=org.datanucleus.api.jpa.PersistenceProviderImpl

You must of course add the DataNucleus libraries to <tomee-home>/lib/ for this to work.

DataNucleus libraries

Jars needed for DataNucleus 4.x:

Add:
<tomee-home>/lib/datanucleus-core-4.1.10.jar
<tomee-home>/lib/datanucleus-api-jpa-4.1.9.jar
<tomee-home>/lib/datanucleus-rdbms-4.1.12.jar

Remove (optional):
<tomee-home>/lib/asm-3.2.jar
<tomee-home>/lib/commons-lang-2.6.jar
<tomee-home>/lib/openjpa-2.2.0.jar (or EclipseLink)
<tomee-home>/lib/serp-1.13.1.jar