3 Replies Latest reply on Feb 14, 2019 12:57 PM by Nicole Müller

    Wildfly 11 multiple persistence units

    Nicole Müller Novice

      Hi,

      I tried to work with more than one persistence-units. I use a process-engine (camunda) which uses a h2 database (persistence-unit primary).

      I want to save some other data in a mysql database. I tried to configure it with a xa datasource because the non-xa datasource does not work. But I still get the error "Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment".

      Here is my code:

       

      <?xml version="1.0" encoding="UTF-8"?>

      <persistence version="2.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_2_0.xsd">

       

        <persistence-unit name="primary">

          <jta-data-source>java:jboss/datasources/ProcessEngine</jta-data-source>

          <properties>

            <!-- Properties for Hibernate -->

            <property name="hibernate.hbm2ddl.auto" value="create-drop" />

            <property name="hibernate.show_sql" value="true" />

          </properties>

        </persistence-unit>

       

         <persistence-unit name="processEntities">

         <provider>org.hibernate.ejb.HibernatePersistence</provider>

          <jta-data-source>java:/ProcessDBXADS</jta-data-source>

          <properties>

            <!-- Properties for Hibernate -->

                  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

                  <property name="hibernate.hbm2ddl.auto" value="update" />

          </properties>

        </persistence-unit>

      </persistence>

       

      I have got a student entity which belongs to the persistence context which uses mysql:

      @Entity

      @Table(name = "student")

      @PersistenceContext(name = "processEntities")

      public class Student extends Person {

       

       

      My EJB:

      @Stateless

      @Named

      public class BPPBusinessLogic {

        @PersistenceContext(name = "processEntities")

        private EntityManager entityManager;

       

      Here are the datasource configurations in my standalone.xml. I tested the connection via the wildfly admin console and it works.

      <xa-datasource jndi-name="java:/ProcessDBXADS" pool-name="ProcessDBXADS" enabled="true" use-ccm="false">

                          <xa-datasource-property name="ServerName">localhost</xa-datasource-property>

                          <xa-datasource-property name="DatabaseName">

                              testprocessdb2

                          </xa-datasource-property>

                          <xa-datasource-property name="PortNumber">

                              3306

                          </xa-datasource-property>

                          <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>

                          <driver>mysql-connector-java-5.1.47-bin.jar_com.mysql.jdbc.Driver_5_1</driver>

                          <security>

                              <user-name>xxxx</user-name>

                              <password>xxxx</password>

                          </security>

                          <validation>

                              <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>

                              <background-validation>true</background-validation>

                              <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>

                          </validation>

                      </xa-datasource>

       

       

       

      <datasource jta="true" jndi-name="java:jboss/datasources/ProcessEngine" pool-name="ProcessEngine" enabled="true" use-java-context="true" use-ccm="true">

                          <connection-url>jdbc:h2:./camunda-h2-dbs/process-engine;DB_CLOSE_DELAY=-1;MVCC=TRUE;DB_CLOSE_ON_EXIT=FALSE</connection-url>

                          <driver>h2</driver>

                          <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>

                          <security>

                              <user-name>sa</user-name>

                              <password>sa</password>

                          </security>

                     </datasource>

       

      Thanks for your help!

      Nicole