Using Eclipselink as a Persistence Provider in a Netbeans/Wildfly project for JTA transaction

Project Versions

 EclipseLink Version: 2.5.2 - [ EclipseLink 2.5.2 == JPA 2.1] 
 JBDC Driver Name             : Oracle JDBC driver
 JDBC Driver Version          :
 Database Version             : Database Product Version: Oracle Database 12c Enterprise Edition Release - 64bit Production
                                With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
                                Advanced Analytics and Real Application Testing options
 Database Name and Instances  : DB Name:  BANKA
                                1. Instance Name: bankA_2 - Host:
                                2. Instance Name: bankA_1 - Host:
 Netbeans Version             : 8.0.2
 Wildfly Version              : 8.2
 Entity Manger Type           : Application Managed
 Transaction Type             : JTA

Setup EclipseLink as a Wildfly Module

Check modules/system/layers/base/org/eclipse/persistence/main directory
oracle@wls1 main]$ pwd

Copy eclipselink-2.5.2.jar from MAVEN repository  to modules/system/layers/base/org/eclipse/persistence/main
[oracle@wls1 main]$ ls
eclipselink-2.5.2.jar  jipijapa-eclipselink-1.0.1.Final.jar  module.xml
Add  eclipselink-2.5.2.jar to module.xml in  modules/system/layers/base/org/eclipse/persistence/main
[oracle@wls1 main]$ cat  module.xml
<module xmlns="urn:jboss:module:1.3" name="org.eclipse.persistence">
        <resource-root path="jipijapa-eclipselink-1.0.1.Final.jar"/>
        <resource-root path="eclipselink-2.5.2.jar"/>
        <module name="asm.asm"/>
        <module name="javax.api"/>
        <module name="javax.annotation.api"/>
        <module name="javax.enterprise.api"/>
        <module name="javax.persistence.api"/>
        <module name="javax.transaction.api"/>
        <module name="javax.validation.api"/>
        <module name="javax.xml.bind.api"/>
        <module name="org.antlr"/>
        <module name="org.apache.commons.collections"/>
        <module name="org.dom4j"/>
        <module name="org.javassist"/>
        <module name=""/>
        <module name="org.jboss.logging"/>
        <module name="org.jboss.vfs"/>

Configure  your pom.xml

  • Remove the hibernate reference and add the EclipseLink reference
As we provided the EclipseLink Jar as a Wildlfly module add : <scope>provided</scope>


Create a new Netbeans project

Create new Maven project : Maven -> Web Application 
Add JSF support          : Properties -> Add Framework -> JSF 2.2 
Source Package -> New  -> Entity Class from Database 
Add New File -> Persistence -> Entity Class from Database 
-> Select Datasource:
 Host : 
 Port : 1521/banka
 URL  :

-> Select table : EMP2
Select ; 
x Generate Named Query Annotations for Persistent Fields
x Generate JAXB Annotations
x Create Persistence Unit

Create persistence.xml 
New File -> Persistence -> Create new Persistence Unit 
-> Select Datasource: ..
x Use Java Transaction API

A working persistence.xml sample may look like :

<?xml version="1.0" encoding="UTF-8"?>
<!-- <persistence xmlns=""
version="2.0"> -->
<persistence version="2.1" xmlns="" 
  <persistence-unit name="jpaPU" transaction-type="JTA">
      <property name="eclipselink.logging.level" value="FINEST"/>
      <property name="eclipselink.cache.shared.default" value="false"/>

  • JTA is used and the datasoruce name is : java:/jboss/datasources/myRacDS
  • org.eclipse.persistence.jpa.PersistenceProvider is the Persistence Provider
  • The perstistence Unit name is jpaPU [ this is used by our JAVA code ]

JAVA Code to create a thread safe access to the Entity Manager

  •   Persistence.createEntityManagerFactory(“jpaPU“) must match  <persistence-unit name=”jpaPU” transaction-type=”JTA“>
  •  <class>com.hhu.wfjpa2el.Logemp2</class>  must match the  Classes we have  created by using Netbeans menu option : Create Entity Class from Database
  • For the Entity Manger access we use the “ThreadLocal Pattern”

        emf = Persistence.createEntityManagerFactory("jpaPU");
        threadLocal = new ThreadLocal<EntityManager>();        
    public static EntityManager getEntityManager() 
        EntityManager em = threadLocal.get();

        if (em == null) 
          // setRunTimeInfo(getRunTimeInfo() + Tools.add_hmtl_pre_tag("Creating Entity Manager ! "  ));
  "Creating Entity Manager Factory ..." );
            em = emf.createEntityManager();
            // set your flush mode here
        return em;
    public static void closeEntityManager() 
        EntityManager em = threadLocal.get();
        if (em != null) 
  "Closing Entity Manager" );
Note for each request you have to open and close the Entity Manager 

A more Complete Code fragment

public String addEntity()  
        EntityManager em;
        String methodName = "addEntity()";
        short eno= getEmpno();
            setRunTimeInfo("Calling "  + methodName + "in progress - ID: " + eno + " - useJoinTransaction : " + isUseJoinTransaction() );          
            Emp2 e =  new Emp2(eno);
            e.setSal(new BigDecimal(1000.0));
            setRunTimeInfo("Requesting Entity Manager.. ");
            setRunTimeInfo("Running em.persists() ... ");           
            setRunTimeInfo("Running em.flush() ... ");
            setRunTimeInfo("Running ut.commit() ... ");
            setRunTimeInfo("Closing  Entity Manager.. !");
            setRunTimeInfo("Leaving " + methodName + " without Exceptions !");                   
          catch ( Throwable t1)
            jpa_exception(t1, methodName);
        setRunTimeInfo("Leaving "  +  methodName + " - Entity manager  closed !\n");
        return "index";

The related Server.log File shoud look like :

2015-05-20 18:38:23,361 INFO  [org.eclipse.persistence] (default task-5) EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
2015-05-20 18:38:23,496 INFO  [org.eclipse.persistence.connection] (default task-5) connecting(DatabaseLogin(
2015-05-20 18:38:23,497 INFO  [org.eclipse.persistence.connection] (default task-5) Connected:
2015-05-20 18:38:23,497 INFO  [org.eclipse.persistence.connection] (default task-5) connecting(DatabaseLogin(
2015-05-20 18:38:23,497 INFO  [org.eclipse.persistence.connection] (default task-5) Connected:
2015-05-20 18:38:23,653 INFO  [org.eclipse.persistence.connection] (default task-5) vfs:/usr/local/wildfly-8.2.0.Final/standalone/deployments/t-1.0.war/WEB-INF/classes/_jpaPU login successful

One thought on “Using Eclipselink as a Persistence Provider in a Netbeans/Wildfly project for JTA transaction”

Leave a Reply

Your email address will not be published. Required fields are marked *