Multiple persistence units in Wildfly?
Asked Answered
Y

3

7

Is it possible to have two persistence units in a Wildfly (9.0.2) application?

I get "WFLYJPA0061: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment deployment "jasper-web.war". Either change the application deployment to have only one persistence unit definition or specify the unitName for each reference to a persistence unit."

I have unitName specified in the @PeristenceContext annotations. I read somewhere I could disable

<!--
<subsystem xmlns="urn:jboss:domain:jpa:1.1">
    <jpa default-datasource="" default-extended-persistence-inheritance="DEEP"/>
</subsystem>
-->

in standalone.xml. This removed the error message, but also disabled injection of entityManagers (null pointer referencing them in code)

I have tried to split the persistence units over two different ejb-jars, each with their own persistence.xml, but as they're still included in the same war, Wildfly still complains.

The the two persistence units are used with hibernate, one with a postgresql database and one with a ucanaccess driver for ms access. They both work separately.

Yukoyukon answered 7/2, 2016 at 20:36 Comment(3)
Yes, it does work. The error indicates that there is an injection point without the unitNamespecified. Are you sure you don't have one missed? If you include the full error it may give more detail as to where it is located.Lautrec
Thanks, I had one without unitName lurking around. Deploys without error now.Yukoyukon
I had the persistence unit name defined and still got that error. Be sure to set the unitName rather than name.Pownall
Y
5

As John Ament pointed out the error message actually indicates an injection point without unitName (that I had forgotten about in my source...) As soon as I got rid of that, everything worked as it should. I also got a bit confused googling this problem, when I actually found some old issues in jboss AS where this actually seems to have been a problem, once.

Yukoyukon answered 8/2, 2016 at 20:23 Comment(1)
While the error message indicates there is such a problem it does not point to the injection point causing the problem. You still have to search for it yourself.Pownall
S
12

here an example of what works in our wildfly 8.x/9.x ejb app:

First of all define all the classes for each persistence-unit in the persistence.xml, unlisted classes can be turned off to disable autodiscovery.

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
   <persistence-unit name="primary">
      <jta-data-source>java:jboss/datasources/primary_ds</jta-data-source>
      <class>whatever.primary.model.SomeEntity</class>
      <exclude-unlisted-classes>true</exclude-unlisted-classes>
      <properties> ... </properties>
   </persistence-unit>

   <persistence-unit name="secondary">
      <jta-data-source>java:jboss/datasources/secondary_ds</jta-data-source>
      <class>whatever.secondary.model.AnotherEntity</class>
      <exclude-unlisted-classes>true</exclude-unlisted-classes>
      <properties> ... </properties>
   </persistence-unit>
</persistence>

If you use JBoss Developer Studio ignore the warning (it is only an eclipse flaw):

Multiple persistence units defined - only the first persistence unit will be recognized

Resources.java

package whatever.util;

import javax.annotation.Resource;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;

public class Resources {

    @Produces
    @PersistenceContext(unitName = "primary")
    private EntityManager emPrimary;

    @Produces
    @PersistenceContext(unitName = "secondary")
    private EntityManager emSecondary;

    @Produces
    @Resource(lookup = "java:jboss/datasources/primary_ds")
    private DataSource dsPrimary;

    @Produces
    @Resource(lookup = "java:jboss/datasources/secondary_ds")
    private DataSource dsSecodnary;

}

Dao primary example

package whatever.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class DaoPrimaryExample {

    @PersistenceContext(unitName = "primary")
    private EntityManager em;

    public void create(SomeEntity entity) {
        em.persist(entity);
    }
}

Dao secondary example

package whatever.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class DaoSecondaryExample {

    @PersistenceContext(unitName = "secondary")
    private EntityManager em;

    public void create(AnotherEntity entity) {
        em.persist(entity);
    }
}

IMPORTANT: If you plan to use booth persistence units in same transaction than XA datasources should be used.

Sangfroid answered 8/2, 2016 at 8:2 Comment(1)
Thank you for a very thorough answer. It certainly works, It was me that had a session bean with a @PersistenceContext without unitName lurking around that I'd forgot about.Yukoyukon
T
12

Adding this option for a persitence unit in the persistence.xml fixed the issue for me:

<property name="wildfly.jpa.default-unit" value="true"/>
Tablet answered 21/3, 2017 at 14:27 Comment(1)
thanks, i'm sure that works too. in the meantime i've switched to sprng boot and have a whole new set of challenges :)Yukoyukon
Y
5

As John Ament pointed out the error message actually indicates an injection point without unitName (that I had forgotten about in my source...) As soon as I got rid of that, everything worked as it should. I also got a bit confused googling this problem, when I actually found some old issues in jboss AS where this actually seems to have been a problem, once.

Yukoyukon answered 8/2, 2016 at 20:23 Comment(1)
While the error message indicates there is such a problem it does not point to the injection point causing the problem. You still have to search for it yourself.Pownall

© 2022 - 2024 — McMap. All rights reserved.