2

I'm getting this problem when I try to deploy a war to a Tomcat 7 server:

IllegalArgumentException: Invalid XML in persistence unit from URL [jar:file:/opt/webapps/foo/WEB-INF/lib/bar.jar!/META-INF/persistence.xml]

SAXParseException; cvc-elt.1: Cannot find the declaration of element 'persistence'.

I've read countless similar questions here, but all the problems were caused by wrong namespaces, mismatching versions, missing elements... My persistence.xml file is correct, as far as I can tell:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
             version="2.1">
  <persistence-unit name="my_pu" transaction-type="RESOURCE_LOCAL">
    <class>com.foo.Bar</class>
    <class>com.foo.Baz</class>
  </persistence-unit>
</persistence>

In fact, the same war works just fine on my PC and on our development environment. It just fails when we promote it to our testing environment. I believe it might be due to xmlns.jcp.org not being reachable from that server, at least I don't get any reply when I try to curl the XSD URL, it just timeouts. Also, it takes the Tomcat around 1 minute to show the error message, which also gives the idea that it's trying to load the remote XSD and getting a timeout.

I've spent the last 5 hours trying to make it take a local XSD file instead, but so far I haven't succeeded. I've tried placing the file...

  • Directly in WEB-INF/.
  • Inside the jar which contains the persistence.xml, in the same folder (META-INF/).
  • Inside the jar, in the WEB-INF/ folder.

And in the persistence.xml:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             version="2.1">

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

<persistence 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 ./persistence.xsd"
             version="2.1">

<persistence>

<persistence version="2.1">

The only thing I've accomplished is to avoid the 1 minute waiting time, if I remove the schemaLocation. But the problem persists. (no pun intended...)

The persistence.xml file is loaded from an applicationContext.xml, in case it makes any difference:

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      id="entityManagerFactory">
  <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
  ...
</bean>

What else can I try?

AJPerez
  • 3,435
  • 10
  • 61
  • 91
  • I forgot to mention, I downloaded the XSD file from here http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd (and renamed it to persistence.xsd, just in case). And its targetNamespace is the same as my declared xmlns. – AJPerez Aug 31 '18 at 06:57
  • These kind of problems usually occurs when the different environments contains different application servers, java versions, etc. Do you have the same JDK and tomcat version in every env? – m4gic Aug 31 '18 at 11:38
  • Yes, the JDK and Tomcat version should be the same in both servers, probably not so in my PC. The difference is that the dev server is maintained by us and doesn't have outgoing traffic restrictions, while the testing server is at our customer's data center, and they cut the requests to xmlns.jcp.org. – AJPerez Sep 02 '18 at 11:45
  • I ran out of time for this issue, so as a workaround, I published the XSD in another Tomcat on the same testing server, and placed the full URL in the XML schemaLocation. It's working now. I'd still prefer to be able to reference the XSD file locally though... – AJPerez Sep 02 '18 at 11:47
  • Good workarund. In this case, just put your xsds to your apps webapps folder and update the link. – m4gic Sep 02 '18 at 12:18

0 Answers0