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

问题描述:

我在WildFly 10.1.0上正确配置了本地postgres数据库。加入我的datasourcestandalone.xml(和删除已亮起standadole.xml以前ExampleDS)和wildfly-10.1.0.Final/modules/system/layers/base/com/postgres/main添加module.xml和JDBC PostgreSQL驱动创建我的Postgres文件夹后,我从./standalone.sh初始化,并要http://127.0.0.1:9990/console/App.html>配置>数据源我成功地ping通我的Postgres数据库(如规定here):如何在Wildfly 10.1.0上正确使用数据源?

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。

+0

Wildfly 10.2.0未发布。你是从开发部门自己构建的吗? –

+0

这是一个错字,对不起 – GabrielRado

错误涉及一个默认数据源:

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.0.Final香草standalone.xml引用ExampleDS数据源作为默认数据源:

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

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

你的persistence.xml数据源引用看起来很好(尽管你没有在standalone.xml中发布你的数据源定义)。

+0

我恢复ExampleDS,它的工作。感谢您的回答。你知道我在哪里可以找到哪些数据源配置应该在'standalone.xml'上,哪些应该在'persistence.xml'上? – GabrielRado

+1

@GabrielRado,Wildfly的发行版包含模式,数据源位于'wildfly-10.1.0.Final/docs/schema/wildfly-datasources_4_0.xsd'。还有:https://docs.jboss.org/author/display/WFLY10/DataSource+configuration。至于persistence.xml,请参阅JPA规范,Hibernate文档,http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd ... –