我在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>
第一个是'默认'单位。它由标准实体管理器使用。第二个是在Java EE环境之外运行JUnit测试所必需的。第三个用于记录独立于JTA事务 这有效。但是当我启动Wildfly时,我甚至会遇到一些错误(尽管我的应用程序中没有使用H2):
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)
仅在部署应用程序时才会发生。
这个想法是通过管理控制台或standalone.xml禁用或删除ExampleDS,但是当我这样做时,我得到一个有错误的启动:
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]"
这到底是怎么回事?为什么这个persistence.xml需要exampleDS? RESOURCE_LOCAL
持久性单元可能配置错误吗? (但如果是这样的话,为什么他们会这样做)。那么这里的问题是什么?
[UPDATE]
当我从hibernate.hbm2ddl.auto
持久性单元中删除RESOURCE_LOCAL
属性时,我发现我没有得到启动错误(启用了exampleDS)。但令我很生气的是,我必须启用Wildfly的exampleDS。否则,Wildfly不会从如上所述的应用程序开始。为什么会如此。这有什么不对?
答案 0 :(得分:0)
您只为第一个持久性单元定义数据源:
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>
这意味着WildFly将默认数据源exampleDS
用于其他持久性单元。
这会导致异常,因为配置的PostgreSQL方言不适用于默认的H2数据源。