Multiple persistence units in Wildfly?

半城伤御伤魂 提交于 2019-12-04 10:28:39

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.

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

<property name="wildfly.jpa.default-unit" value="true"/>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!