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

时间:2017-09-16 20:38:16

标签: postgresql hibernate intellij-idea wildfly-10

我已在WildFly 10.1.0上正确配置了本地postgres数据库。在datasource上添加我的standalone.xml(并删除之前已在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>配置> DataSources我成功地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。

1 个答案:

答案 0 :(得分:1)

该错误涉及默认数据源:

  

jboss.naming.context.java.module.artifactId-1.artifactId-1.DefaultDataSource   缺少[jboss.naming.context.java.jboss.datasources.ExampleDS]&#34;]}

您删除了ExampleDS:

  

之后...并删除已经出现在standadole.xml上的上一个ExampleDS

但Wildfly 10.1.0.Final附带的vanilla standalone.xml引用了ExampleDS数据源作为默认数据源:

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

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

您的persistence.xml数据源引用似乎很好(尽管您没有在standalone.xml中发布数据源定义)。