Search code examples
javajpadeploymentwildflyear

How to share JPA data model between 2 EAR in JBoss "Wildfly"


We have a working JBoss v8 "Wildfly" deployment like this:

App-ear.ear
|_App-model.jar
|_App-ejb.jar
|_App-web.war

This deployment comes with a persistence.xml inside the App-model.jar. Now I want to deploy a second EAR with a different WAR module that I can re-deploy independent from the original application. Therefor I've created a second EAR like this:

Second-ear.ear
|_Second-web.war

The second application has a classpath dependency to App-model.jar during development in Eclipse (setup as Maven projects) and builds just fine. To make that dependency available during runtime I've edited Second-ear.ear/META-INF/jboss-deployment-structure.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>

        <dependencies>
            <module name="deployment.App-ear.ear.App-model.jar" />   
        </dependencies>

    </deployment>
</jboss-deployment-structure>

But the JBoss AS fails to load the persistence with the following error:

2015-08-19 09:13:36,903 ERROR [] [ @ ] [] [MSC service thread 1-5] [fail] MSC000001: Failed to start service jboss.deployment.unit."Second-ear.ear".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."Second-ear.ear".WeldStartService: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_25]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_25]
Caused by: java.lang.IllegalArgumentException: JBAS016069: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named MyPersistenceUnitName in deployment Second-ear.ear
    at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:110)
    at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.registerPersistenceUnitInjectionPoint(WeldJpaInjectionServices.java:90)
    at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceUnitResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:279)
    at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceUnitResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:267)
    at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createFieldResourceInjection(ResourceInjectionFactory.java:207)
    at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createResourceInjections(ResourceInjectionFactory.java:182)
    at org.jboss.weld.injection.ResourceInjectionFactory.discoverType(ResourceInjectionFactory.java:405)
    at org.jboss.weld.injection.ResourceInjectionFactory.getResourceInjections(ResourceInjectionFactory.java:92)
    at org.jboss.weld.injection.producer.ResourceInjector.<init>(ResourceInjector.java:59)
    at org.jboss.weld.injection.producer.ResourceInjector.of(ResourceInjector.java:49)
    at org.jboss.weld.injection.producer.BeanInjectionTarget.<init>(BeanInjectionTarget.java:62)
    at org.jboss.weld.injection.producer.BeanInjectionTarget.createDefault(BeanInjectionTarget.java:46)
    at org.jboss.weld.manager.InjectionTargetFactoryImpl.chooseInjectionTarget(InjectionTargetFactoryImpl.java:128)
    at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:87)
    at org.jboss.weld.bean.ManagedBean.<init>(ManagedBean.java:91)
    at org.jboss.weld.bean.ManagedBean.of(ManagedBean.java:71)
    at org.jboss.weld.bootstrap.AbstractBeanDeployer.createManagedBean(AbstractBeanDeployer.java:264)
    at org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:228)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:78)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:75)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_25]
    ... 3 more

The CDI Bean that's trying to use that persistence unit injects an EntityManager produced by the original App-model.jar:

@Named("test")
public class Test {

    @Inject
    @MyModelDB // Qualifier
    private EntityManager em = null;

    // A simple test of the datamodel...
    public Long numberOfProjects() {
        if (this.em != null) {
            return ((BigDecimal) this.em.createNativeQuery("select count(*) from e_bo_projects").getSingleResult()).longValue();
        }
        return -1L;
    }
}

How can I solve this problem?


Solution

  • I've successfully deployed my two EARs now... thanks for your comments to my original question. My initial approach with 2 EAR including the same data-model JAR works, if the persistence.xml doesn't include the JNDI binding. Thanks @Franck! The mentioned jboss-deployment-structure.xml does no longer refer to my other EAR.

    So now my deployment looks like this:

    App-ear.ear
    |_App-model.jar
    |_App-ejb.jar
    |_App-web.war
    
    Second-ear.ear
    |_App-model.jar
    |_Second-web.war
    

    This works for both applications and I've successfully accessed the database in both cases.