Java 如何在wildfly 10中配置数据源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33081524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:17:17  来源:igfitidea点击:

How to configure datasource in wildfly 10?

javaxmlpostgresqldatasourcewildfly

提问by gstackoverflow

I am starting introduction with wildfly learning. I have downloaded distribution of server.
Now I am trying to configure datasource but I see following error:

我开始介绍野蝇学习。我已经下载了服务器的分发版。
现在我正在尝试配置数据源,但我看到以下错误:

Unexpected HTTP response: 500

Request
{
    "address" => [
        ("subsystem" => "datasources"),
        ("data-source" => "PostgreDataSource")
    ],
    "operation" => "test-connection-in-pool"
}

Response

Internal Server Error
{
    "outcome" => "failed",
    "failure-description" => "WFLYJCA0040: failed to invoke operation: WFLYJCA0042: failed to match pool. Check JndiName: java:jboss/datasources/PostgreDataSource",
    "rolled-back" => true
}

My steps:
1. Created folder wildfly-10.0.0.CR2\wildfly-10.0.0.CR2\modules\org\postgres\mainand copy postgresql-9.0-801.jdbc4.jarfrom \.m2\repository\postgresql\postgresql\9.0-801.jdbc4there.

我的步骤:
1. 创建文件夹wildfly-10.0.0.CR2\wildfly-10.0.0.CR2\modules\org\postgres\mainpostgresql-9.0-801.jdbc4.jar\.m2\repository\postgresql\postgresql\9.0-801.jdbc4那里复制。

2.Created module.xml(inside wildfly-10.0.0.CR2\wildfly-10.0.0.CR2\modules):

2.创建module.xml(里面wildfly-10.0.0.CR2\wildfly-10.0.0.CR2\modules):

with following content:

具有以下内容:

<module xmlns="urn:jboss:module:1.0" name="org.postgres"> 
  <resources> 
    <resource-root path="postgresql-9.0-801.jdbc4.jar"/> 
  </resources> 
   <dependencies> 
     <module name="javax.api"/> 
     <module name="javax.transaction.api"/> 
   </dependencies> 
</module> 
  1. Modified standalone.xml(wildfly-10.0.0.CR2\wildfly-10.0.0.CR2\standalone\configuration) like this(sorry - I don't know how to copy xml that it can be visible for another users(full content visible here: http://collabedit.com/psk4a)):
  1. 像这样修改standalone.xml( wildfly-10.0.0.CR2\wildfly-10.0.0.CR2\standalone\configuration)(抱歉 - 我不知道如何复制 xml 使其对其他用户可见(完整内容在此处可见:http: //collabedit.com/psk4a)):

Please help to understand what do I wrong?

请帮助理解我做错了什么?

enter image description here

在此处输入图片说明

采纳答案by gstackoverflow

Problem resolved after move module.xmlto wildfly-10.0.0.CR2\wildfly-10.0.0.CR2\modules\org\postgres\main

问题后招解决module.xml,以wildfly-10.0.0.CR2\wildfly-10.0.0.CR2\modules\org\postgres\main

回答by Gimby

I am not 100% positive, but if I see some links around the net it may be caused by default pool settings. You might try by adding specific pool configuration settings to your datasource:

我不是 100% 肯定,但如果我在网上看到一些链接,可能是由默认池设置引起的。您可以尝试向数据源添加特定的池配置设置:

<datasource jndi-name="blabla"... >
  <pool>  
    <min-pool-size>1</min-pool-size>  
    <max-pool-size>20</max-pool-size>  
    <prefill>true</prefill>            
  </pool>  
</datasource>

I base that on this stackoverflow thread: WildFly jdbc connection with Oracle

我基于这个stackoverflow线程:WildFly jdbc connection with Oracle

where an answer links to this related JBoss forum thread: https://developer.jboss.org/thread/257721

答案链接到这个相关的 JBoss 论坛线程:https: //developer.jboss.org/thread/257721

The pool settings are mentioned in the Wildfly configuration documentation by the way. This is Wildfly 9, but I can't imagine things changed much in Wildfly 10: https://docs.jboss.org/author/display/WFLY9/DataSource+configuration

顺便说一下,Wildfly 配置文档中提到了池设置。这是 Wildfly 9,但我无法想象 Wildfly 10 中的变化很大:https://docs.jboss.org/author/display/WFLY9/DataSource+configuration

回答by Ajay Kumar

Below given is driver configuration and data source creation and how to make it globally visible so that all J2EE deployments can access the particular module if needed.

下面给出了驱动程序配置和数据源创建以及如何使其全局可见,以便所有 J2EE 部署可以在需要时访问特定模块。

1. PostGreSQL Driver Configuration

1.PostGreSQL驱动配置

Create directory structure as below inside the modules in wildfly-8.2.0.Final\modules directory and place the mentioned files and driver jar. Directory: wildfly-8.2.0.Final\modules\org\postgresql\main

在wildfly-8.2.0.Final\modules 目录下的modules 中创建如下目录结构,并放置提到的文件和驱动程序jar。目录:wildfly-8.2.0.Final\modules\org\postgresql\main

File: module.xml

文件:module.xml

    <!--<?xml version="1.0" encoding="UTF-8"?>-->
    <module xmlns="urn:jboss:module:1.0" name="org.postgresql">
        <resources>
            <resource-root path="postgresql-9.4-1204.jdbc41.jar"/>
        </resources>
        <dependencies><module name="javax.api"/></dependencies>
    </module>

JAR : PostGreSQL Driver: postgresql-9.4-1204.jdbc41.jar

JAR:PostGreSQL 驱动程序:postgresql-9.4-1204.jdbc41.jar

Note : Driver version can be your choice and please ensure to reflect that version name in module.xml file. Please note that the driver name="org.postgresql” mentioned in the module.xml file should be matching with the data source(s) configuration in the standalone.xml file.

注意:驱动程序版本可以由您选择,请确保在 module.xml 文件中反映该版本名称。请注意,module.xml 文件中提到的驱动程序名称="org.postgresql" 应与 standalone.xml 文件中的数据源配置匹配。

Note: The PostGreSQL driver version should be compatible to the java version in the system. In this example, java is 1.7 & PostGreSQL driver used is postgresql-9.4-1204.jdbc41.jar.

注意:PostGreSQL 驱动版本应与系统中的java 版本兼容。在本例中,java 为 1.7,使用的 PostGreSQL 驱动程序为 postgresql-9.4-1204.jdbc41.jar。

2. Configuring the DataSources

2. 配置数据源

Datasources are configured in the standalone.xml file in the WildFly 8.2.0.Final\standalone\configuration. As the first step configure the PostGreSQL driver reference in the standalone.xml file as below inside the tag

数据源在 WildFly 8.2.0.Final\standalone\configuration 中的 standalone.xml 文件中配置。作为第一步,在standalone.xml 文件中配置PostGreSQL 驱动程序引用,如下所示

<driver name="postgresql" module="org.postgresql">
<datasource-class>org.postgresql.Driver</datasource-class>
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>

1. Add the datasource details:

1. 添加数据源详细信息:

Please add this inside tag

请添加这个内部标签

<datasource jndi-name="java:/db1" pool-name="db1" enabled="true" use-java-context="true">
<connection-url>jdbc:postgresql://localhost:5432/dbname</connection-url>
<driver>postgresql</driver>
<security>
    <user-name>user_name</user-name>
    <password>password</password>
</security>
</datasource>

2.make the published drivers globally visible by adding to the section

2.通过添加到该部分,使已发布的驱动程序全局可见

Here it is:

这里是:

<global-modules>
            <module name="org.postgresql" slot="main"/>
</global-modules>

Note : Global modules is a set of JBoss Modules that will be added as dependencies to the JBoss Module of every Java EE deployment. Such dependencies allows Java EE deployments to see the classes exported by the global modules. Refer : https://docs.jboss.org/author/display/WFLY8/Subsystem+configuration

注意:全局模块是一组 JBoss 模块,它们将作为依赖项添加到每个 Java EE 部署的 JBoss 模块中。此类依赖项允许 Java EE 部署查看全局模块导出的类。参考:https: //docs.jboss.org/author/display/WFLY8/Subsystem+configuration

Once configured the above, please start your WildFly instance.

完成上述配置后,请启动您的 WildFly 实例。

回答by gleitonfranco

I'd like to say that I solved the problem only by using wildfly admin console (http://localhost:9990/console/). Not so spartan solution, but it works. I only pointed the JDBC driver jar from external directory and after I created a datasource. No xml by hand and no modifying wildfly's directory structure. For java 8 + postgresql 9.5 I used postgresql-42.1.1.jar. I had problems befor only because I chose the wrong driver and database's name was wrong.

我想说我只是通过使用 wildfly 管理控制台(http://localhost:9990/console/)解决了这个问题。不是那么简陋的解决方案,但它有效。我只在创建数据源之后从外部目录指向 JDBC 驱动程序 jar。无需手工编写 xml,也无需修改 Wildfly 的目录结构。对于 java 8 + postgresql 9.5,我使用了 postgresql-42.1.1.jar。我之前遇到问题只是因为我选择了错误的驱动程序和数据库名称错误。