0

I have three datasources configured in my persistence.xml:

<?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="MyPersistenceUnit" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/myDS</jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.useUnicode" value="true" />
            <property name="hibernate.connection.characterEncoding" value="UTF-8" />
            <property name="hibernate.connection.charSet" value="UTF-8" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
        </properties>
    </persistence-unit>

    <persistence-unit name="MyPersistenceTestUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.useUnicode" value="true" />
            <property name="hibernate.connection.characterEncoding" value="UTF-8" />
            <property name="hibernate.connection.charSet" value="UTF-8" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mydb" />
            <property name="hibernate.connection.username" value="myuser" />
            <property name="hibernate.connection.password" value="mypass" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
        </properties>
    </persistence-unit>

    <persistence-unit name="MyLoggingUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.useUnicode" value="true" />
            <property name="hibernate.connection.characterEncoding" value="UTF-8" />
            <property name="hibernate.connection.charSet" value="UTF-8" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mydb" />
            <property name="hibernate.connection.username" value="myuser" />
            <property name="hibernate.connection.password" value="mypass" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
        </properties>
    </persistence-unit>

</persistence>

The first is the 'default' unit. It's used by the standard entity manager. The second is needed for running JUnit tests outside of a Java EE environment. The third one is used for logging to be independent from JTA transactions.
This works. But when I start Wildfly, I even get some of these errors (although I don't use H2 in my application):

21:32:33,748 ERROR [org.hibernate.tool.hbm2ddl.SchemaValidator] (ServerService Thread Pool -- 51) HHH000319: Could not get database metadata: org.h2.jdbc.JdbcSQLException: Table "PG_CLASS" not found; SQL statement:
select relname from pg_class where relkind='S' [42102-173]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)

It only happens when the application is deployed.

The idea was to disable or remove the ExampleDS by admin console or in standalone.xml, but when I do that, I get a startup with errors:

21:36:20,992 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "com.zonacroft.mycuisine.webserver-TRUNK.war")]) - failure description: {"JBAS014771: Services with missing/unavailable dependencies" => [
    "jboss.persistenceunit.\"com.mydomain.myapp.webserver-TRUNK.war#MyLoggingUnit\".__FIRST_PHASE__ is missing [jboss.data-source.java:jboss/datasources/ExampleDS]",
    "jboss.persistenceunit.\"com.mydomain.myapp.webserver-TRUNK.war#MyPersistenceTestUnit\".__FIRST_PHASE__ is missing [jboss.data-source.java:jboss/datasources/ExampleDS]"

What on earth is going on here? Why does this persistence.xml need the exampleDS? Are the RESOURCE_LOCAL persistence units maybe wrong configured? (But if so, why do they work then). So what's the problem here?

[UPDATE]
I figured out that I don't get startup errors (with exampleDS enabled) when I remove the hibernate.hbm2ddl.auto properties from the RESOURCE_LOCAL persistence units. But it annoys me that I have to let the exampleDS of Wildfly enabled. Otherwise Wildfly would not start with the application as described above. Why is this so. What's wrong here?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Bevor
  • 8,396
  • 15
  • 77
  • 141
  • 1
    Can't help with your specific question, but `ExampleDS` is used for the platform default datasource, a new feature introduced in Java EE 7. I'm personally not a fan of using resource local PUs for unit tests. IMHO testing with the real persistence unit (e.g. using Arquillian) allows for more realistic testing. – Arjan Tijms Aug 24 '15 at 07:14

1 Answers1

0

You only define a datasource for the first persistence unit:

<jta-data-source>java:jboss/datasources/myDS</jta-data-source>

That means WildFly uses the default datasource exampleDS for your other persistence units.

This causes the exception, since the configured PostgreSQL dialect does not work for the default H2 datasource.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
  • That's true, but I can't set a non-jta-datasource (since other errors occur then) and I don't need one because the needed parameters are set in the persistence units and it's a local resource. So what's the real solution for this now? – Bevor Aug 25 '15 at 07:16
  • As I tried to test my logging, it didn't work at all (in JEE environment). But the logging PU works when I use the properties of the JTA datasource with the same datasource but as non-jta-datasource and RESOURCE_LOCAL as transaction type. Here also works 'validate'. I'm not sure if I understand completely why the first definition didn't work, but that's another question. – Bevor Aug 26 '15 at 18:52