How to add Datanucleus Module in Wildfly

How do you add a Module in Wildfly J2EE server?

The Wildfly (www.wildfly.org) Java EE server has concept of “Module” to add external libraries. This involve creating a directory to deploy the jars and a xml file that describes the library.

Following is the example of adding Datanucleus JPA jars in the Wildfly version 10.

  1. Create a directory to store the DataNucleus libraries, in
    $JBOSS_HOME/modules/system/layers/base/org/datanucleus/main
  2. Add the following jars from the lib directory:
    datanucleus-api-jpa-XXX.jar,
    datanucleus-core-XXX.jar,
    datanucleus-rdbms-XXX.jar,
    datanucleus-jpa-query-XXX.jar
    
  3. Add a “module.xml” file in the $JBOSS_HOME/modules/org/datanucleus/main directory like this
    <module xmlns="urn:jboss:module:1.1" name="org.datanucleus">
    <dependencies>
    <module name="javax.api"/>
    <module name="javax.persistence.api"/>
    <module name="javax.transaction.api"/>
    <module name="javax.validation.api"/>
    </dependencies>
    <resources>
    <resource-root path="datanucleus-api-jpa-xxxxx.jar"/>
    <resource-root path="datanucleus-core-xxxxx.jar"/>
    <resource-root path="datanucleus-rdbms-xxx.jar"/>
    <resource-root path="datanucleus-jpa-query-xxxx.jar"/>
    </resources>
    </module>
  4. Example of Datanucleus persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="[Persistence Unit Name]" transaction-type="JTA">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
        <!-- MySQL DS -->
        <jta-data-source>java:/jdbc/simple</jta-data-source>
        <non-jta-data-source>java:/jdbc/simple-nonjta</non-jta-data-source>

        <class>[Entities must be listed here]</class>

        <properties>
            <!-- Magic JBoss property for specifying the persistence provider -->
            <property name="jboss.as.jpa.providerModule" value="org.datanucleus"/>

            <!-- following is probably not useful... but it ensures we bind to the JTA transaction manager...-->
            <property name="datanucleus.jtaLocator" value="custom_jndi"/>
            <property name="datanucleus.jtaJndiLocation" value="java:/TransactionManager"/>

            <property name="datanucleus.autoCreateSchema" value="true"/>
            <property name="datanucleus.metadata.validate" value="false"/>
            <property name="datanucleus.validateTables" value="false"/>
            <property name="datanucleus.validateConstraints" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

Second way of adding your application libraries?

Besides adding module in Wildfly, you may add those libraries in your application war file. If you are using Maven, add dependency in your parent pom. When war is built, those libraries will be included in the WEB-INF/lib.