简体   繁体   English

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

[英]How to configure datasource in wildfly 10?

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\\main and copy postgresql-9.0-801.jdbc4.jar from \\.m2\\repository\\postgresql\\postgresql\\9.0-801.jdbc4 there. 1. 创建文件夹wildfly-10.0.0.CR2\\wildfly-10.0.0.CR2\\modules\\org\\postgres\\main并从\\.m2\\repository\\postgresql\\postgresql\\9.0-801.jdbc4复制postgresql-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 )):像这样修改了standalone.xmlwildfly-10.0.0.CR2\\wildfly-10.0.0.CR2\\standalone\\configuration )(对不起 - 我不知道如何复制它可以被其他用户看到的xml(完整内容)在这里可见: http : //collabedit.com/psk4a )):

Please help to understand what do I wrong?请帮助理解我做错了什么?

在此处输入图片说明

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.在wildfly-8.2.0.Final\\modules 目录下的modules 中创建如下目录结构,并放置提到的文件和驱动程序jar。 Directory: wildfly-8.2.0.Final\\modules\\org\\postgresql\\main目录: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.注意:驱动程序版本可以由您选择,请确保在 module.xml 文件中反映该版本名称。 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 文件中提到的驱动程序名称="org.postgresql" 应与 standalone.xml 文件中的数据源配置匹配。

Note: The PostGreSQL driver version should be compatible to the java version in the system.注意:PostGreSQL 驱动版本应与系统中的java 版本兼容。 In this example, java is 1.7 & PostGreSQL driver used is postgresql-9.4-1204.jdbc41.jar.在本例中,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.数据源在 WildFly 8.2.0.Final\\standalone\\configuration 中的 standalone.xml 文件中配置。 As the first step configure the PostGreSQL driver reference in the standalone.xml file as below inside the tag作为第一步,在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.注意:全局模块是一组 JBoss 模块,它们将作为依赖项添加到每个 Java EE 部署的 JBoss 模块中。 Such dependencies allows Java EE deployments to see the classes exported by the global modules.此类依赖项允许 Java EE 部署查看全局模块导出的类。 Refer : https://docs.jboss.org/author/display/WFLY8/Subsystem+configuration参考: https : //docs.jboss.org/author/display/WFLY8/Subsystem+configuration

Once configured the above, please start your WildFly instance.完成上述配置后,请启动您的 WildFly 实例。

module.xml移动到wildfly-10.0.0.CR2\\wildfly-10.0.0.CR2\\modules\\org\\postgres\\main后问题解决

I am not 100% positive, but if I see some links around the net it may be caused by default pool settings.我不是 100% 肯定,但如果我在网上看到一些链接,可能是由默认池设置引起的。 You might try by adding specific pool configuration settings to your datasource:您可以尝试向数据源添加特定的池配置设置:

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

I'd like to say that I solved the problem only by using wildfly admin console ( http://localhost:9990/console/ ).我想说我只是通过使用 wildfly 管理控制台( 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.我只在创建数据源之后从外部目录指向 JDBC 驱动程序 jar。 No xml by hand and no modifying wildfly's directory structure.无需手工编写 xml,也无需修改 Wildfly 的目录结构。 For java 8 + postgresql 9.5 I used postgresql-42.1.1.jar.对于 java 8 + postgresql 9.5,我使用了 postgresql-42.1.1.jar。 I had problems befor only because I chose the wrong driver and database's name was wrong.我之前遇到问题只是因为我选择了错误的驱动程序和数据库的名称是错误的。

Although this topic is a bit old, I'd like to stress that the recommended way to install a datasource as a module is using the CLI 'module add' command which creates the full path for the module and the module.xml configuration file.虽然这个主题有点老,但我想强调的是,将数据源安装为模块的推荐方法是使用 CLI 的“module add”命令,该命令创建模块的完整路径和 module.xml 配置文件。 This makes the whole process less error-prone.这使得整个过程不易出错。

module add --name=org.postgres --resources=[JDBC JAR FILE] --dependencies=javax.api,javax.transaction.api

Also, I see mentioned here the usage of global modules for datasource.另外,我看到这里提到了数据源的全局模块的使用。 This is not a common practice for JDBC drivers as global modules are meant to be used for shared libraries which are common to all applications and don't require maintenance.这不是 JDBC 驱动程序的常见做法,因为全局模块旨在用于所有应用程序通用且不需要维护的共享库。 You might end up with an unconsistent datasource configuration if the Database is upgraded and you forget to update the JDBC Driver in your modules configuration.如果升级了数据库并且您忘记更新模块配置中的 JDBC 驱动程序,您最终可能会得到不一致的数据源配置。

Ref: How to configure a Datasource in WildFly参考: 如何在 WildFly 中配置数据源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM