如何在 Wildfly 10.1.0 上正确使用数据源?

How to correctly use datasource on Wildfly 10.1.0?

我已经在 WildFly 10.1.0 上正确配置了本地 postgres 数据库。在 standalone.xml 上添加我的 datasource(并删除之前 standadole.xml 上已经出现的 ExampleDS)并在 wildfly-10.1.0.Final/modules/system/layers/base/com/postgres/main 上创建我的 postgres 文件夹后添加 module.xml 和 JDBC postgresql 驱动程序,我从 ./standalone.sh 初始化它并转到 http://127.0.0.1:9990/console/App.html > 配置 > 数据源 我成功地 ping 我的 postgres 数据库(如 here 所述):

Successfully created JDBC connection.
Successfully connected to database Postgres.

现在我想使用 Intellij 在我的项目上配置 Hibernate。问题是我不知道如何正确设置 persistence.xml 来使用我之前配置的数据源。我有这个 pom.xml:

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.1.4</version>
    </dependency>

然后我有这个 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="unitName">
    <jta-data-source>java:jboss/datasources/Postgres-DS</jta-data-source>
    <class>entity.PersistentEntity.Account</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>

但是当我通过 Intellij 将我的项目上传到 WildFly 时,出现了这个错误:

17:18:06,602 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 23) HHH000228: Running hbm2ddl schema update
17:18:06,675 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 2) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "artifactId-1")]) - failure description: {
"WFLYCTL0412: Required services that are not installed:" => ["jboss.naming.context.java.jboss.datasources.ExampleDS"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => ["jboss.naming.context.java.module.artifactId-1.artifactId-1.DefaultDataSource is missing [jboss.naming.context.java.jboss.datasources.ExampleDS]"]
}
17:18:06,677 ERROR [org.jboss.as.server] (management-handler-thread - 2) WFLYSRV0021: Deploy of deployment "artifactId-1.war" was rolled back with the following failure message: 
{
"WFLYCTL0412: Required services that are not installed:" => ["jboss.naming.context.java.jboss.datasources.ExampleDS"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => ["jboss.naming.context.java.module.artifactId-1.artifactId-1.DefaultDataSource is missing [jboss.naming.context.java.jboss.datasources.ExampleDS]"]
}

原来的数据源是这样的:

 <datasources>
            <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
                <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
                <driver>h2</driver>
                <security>
                    <user-name>sa</user-name>
                    <password>sa</password>
                </security>
            </datasource>
            <drivers>
                <driver name="h2" module="com.h2database.h2">
                    <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                </driver>
            </drivers>
        </datasources>

为什么还在尝试使用原来的数据源?我也应该改变 standalone-full.xml 吗?在 standalone-full.xml 上仍然没有 postgres 数据源,只有上面的这个 h2。

错误涉及默认数据源:

jboss.naming.context.java.module.artifactId-1.artifactId-1.DefaultDataSource is missing [jboss.naming.context.java.jboss.datasources.ExampleDS]"] }

您删除了 ExampleDS:

after ... and deleting the previous ExampleDS that already comes on standadole.xml

但是 Wildfly 10.1 附带的香草 standalone.xml。0.Final 引用 ExampleDS 数据源作为默认数据源:

<subsystem xmlns="urn:jboss:domain:ee:4.0">
    <default-bindings
        ...
        datasource="java:jboss/datasources/ExampleDS"
        ...

你可能没有解决这个问题。

您的 persistence.xml 数据源引用似乎没问题(尽管您没有 post 在 standalone.xml 中定义数据源)。