我在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
持久性单元是否配置错误?(但如果是这样,为什么它们会起作用呢?)那么问题是什么呢?
我发现,当我从RESOURCE_LOCAL
持久性单元中删除hibernate.hbm2ddl.auto
属性时,我不会得到启动错误(启用了exampleDS)。但它惹恼了我,我不得不让Wildfly的exampleDS启用。否则,Wildfly不会像上面描述的那样启动应用程序。为什么会这样呢?这是怎么了?
您只能为第一个持久单元定义一个数据源:
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>
这意味着WildFly为您的其他持久性单元使用默认的数据源exampleDS
。
这会导致异常,因为配置的PostgreSQL方言不能用于默认的H2数据源。