Fixing "Could not resolve a persistence unit..." errors when PU is specified, found
Asked Answered
H

3

8

I'm running Glassfish 3.1-SNAPSHOT as of today (2010-11-12).

I'm using the embedded EJBContainer.

On the classpath, as reported by the EJBContainer, I have a META-INF/persistence.xml. This file defines two persistence units: one called "ngp" and one called "cx".

Debugging output shows that the Glassfish JPA deployer finds it, and recognizes both the cx PU and the ngp PU.

The EJBContainer bombs out with the following all-too-common JPA error:

java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [cx] in the scope of the module called [/Users/ljnelson/Projects/foo/target/test-classes/]. Please verify your application.
at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:693)
at com.sun.enterprise.deployment.EjbBundleDescriptor.findReferencedPUs(EjbBundleDescriptor.java:910)
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:140)
at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:869)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:410)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.kernel.embedded.EmbeddedDeployerImpl.deploy(EmbeddedDeployerImpl.java:193)
at org.glassfish.kernel.embedded.EmbeddedDeployerImpl.deploy(EmbeddedDeployerImpl.java:142)
at org.glassfish.ejb.embedded.EJBContainerImpl.deploy(EJBContainerImpl.java:135)
at org.glassfish.ejb.embedded.EJBContainerProviderImpl.createEJBContainer(EJBContainerProviderImpl.java:132)
at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:127)

I stress again that the deployment logs show that at least the deployer encounters both persistence units.

The class that wants to use the "cx" PU contains the usual boilerplate:

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

The persistence.xml is present in (the usual Maven place) target/test-classes/META-INF and looks like this:

<?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="cx" transaction-type="JTA">
        <jta-data-source>java:global/jdbc/H2Test</jta-data-source>
        <!-- snip -->
    </persistence-unit>

    <persistence-unit name="ngp" transaction-type="JTA">
        <jta-data-source>java:global/jdbc/H2Test</jta-data-source>
        <!-- snip -->
    </persistence-unit>

</persistence>

The Glassfish embedded EJBContainer, while doing its work, outputs this:

Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINE: Got com.sun.enterprise.deployment.node.PersistenceUnitNode@2026c088
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINER: With attribute name
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINER: With value cx
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINE: in class com.sun.enterprise.deployment.PersistenceUnitDescriptor  method  setName with  cx
...snip...
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINE: Got com.sun.enterprise.deployment.node.PersistenceUnitNode@1648ff68
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINER: With attribute name
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINER: With value ngp
Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
FINE: in class com.sun.enterprise.deployment.PersistenceUnitDescriptor  method  setName with  ngp

Troubleshooting recipes, anyone?

Hyperon answered 12/11, 2010 at 17:58 Comment(5)
I should mention I'm using JTA--container managed transactions--in both PUs. It occurs to me that XA would be a requirement here. Would the fact that my test data sources are not XA compliant be cause for this error?Hyperon
Updated the datasources to use XA; no effect.Hyperon
Whether the data sources are XA or not shouldn't matter, at least not now (it might be later if you decide to use both EM at the same time but that's another story). Does it work with only one PU?Rodrigorodrigue
Yes. Well, it's hard to tell what part is responsible for this error. I have, in the classpath, an AbstractDAO with an em that references the "ngp" persistence unit, so that unit needs to be there for injection. But nothing in the test case uses that entity manager, and the bean under test does not extend from AbstractDAO. This setup DID work with an ancient build of Glassfish embedded--3.0.1-b02 if memory serves.Hyperon
Time, I guess, to waste the rest of the afternoon boiling down a test case and filing a Glassfish bug!Hyperon
H
7

This one is a combination of weird behavior and pilot error.

First, the pilot error.

The particular JUnit test case I was looking at was that of a colleague, and it was named as though it were an EJB itself, following our internal naming convention. This is probably a cut and paste error on my colleague's part.

I mention that because every time I opened the file I stared at it as though it itself were an EJB.

But of course it is not an EJB.

However, mysteriously, there is a @PersistenceContext annotation in there, and an EntityManager, which is unused. The persistence context has an attribute of--you guessed it--unitName="cx".

So the strange behavior is that somewhere in between the old EJB container, which ran this test case fine, and now, the EJB container began treating this non-EJB, non-special class as a valid target for @PersistenceContext injection. Perhaps this test case is being treated as a managed bean, but I was under the impression that managed beans in a non-CDI environment had to be annotated as such.

Anyhow, once I removed this spurious @PersistenceContext annotation, everything worked fine.

Hyperon answered 12/11, 2010 at 21:29 Comment(2)
This explains a big part of the problem. However, the change between GF 3.0.1 and GF 3.1 is indeed weird (and there might be a bug somewhere). Is your archive even CDI enabled (does it have a beans.xml)?Rodrigorodrigue
No, it is deliberately NOT CDI-enabled, as Glassfish has had all sorts of horrible problems with CDI in the past.Hyperon
H
5

If by mistake you put @PersistenceContext(name="cx") instead of @PersistenceContext(unitName="cx") you get the same error with everything else working.

Hospers answered 23/7, 2014 at 16:6 Comment(2)
Welcome to SO. For future reference you should consider giving a more detailed and complete answer, otherwise you might get downvoted/flaggged. :) Happy coding.Olnay
Granted, the answer is a little short, but it was just the solution for my problem.Sutton
T
1

I faced the same problem. The error was in the name of one of my unitName

unitName="PUname error in one of my classes"
Tubercular answered 10/6, 2018 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.