Java 如何将 PostgreSQL 数据源添加到 WildFly 9.0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31222860/
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 10:48:54  来源:igfitidea点击:

How to add PostgreSQL datasource to WildFly 9.0?

javapostgresqljdbcdatasourcewildfly

提问by czerny

I've tried tutorial at mastertheboss.com:

我在mastertheboss.com尝试过教程:

  1. ./jboss-cli.sh
  2. module add --name=org.postgres --resources=/tmp/postgresql-9.3-1101.jdbc41.jar --dependencies=javax.api,javax.transaction.api
  3. /subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver)
  4. data-source add --jndi-name=java:/PostGreDS --name=PostgrePool --connection-url=jdbc:postgresql://localhost/postgres --driver-name=postgres --user-name=postgres --password=postgres
  1. ./jboss-cli.sh
  2. module add --name=org.postgres --resources=/tmp/postgresql-9.3-1101.jdbc41.jar --dependencies=javax.api,javax.transaction.api
  3. /subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver)
  4. data-source add --jndi-name=java:/PostGreDS --name=PostgrePool --connection-url=jdbc:postgresql://localhost/postgres --driver-name=postgres --user-name=postgres --password=postgres

This tutorial works with WildFly 8.2, but it doesn't work with WildFly 9.0. 3rd step fails with error message:

本教程适用于 WildFly 8.2,但不适用于 WildFly 9.0。第 3 步失败并显示错误消息:

{
"outcome" => "failed",
"failure-description" => "WFLYJCA0041: Failed to load module for driver [org.portgres]",
"rolled-back" => true
}

How to add Postgres datasource to WildFly 9.0?

如何将 Postgres 数据源添加到 WildFly 9.0?

采纳答案by ragelh

I've encountered the same error and behavior of WildFly 9. I'm a complete newbie to WF, but after some research I've found that the trouble is in the module naming. If I'm getting it well, the actual package names in the module are used to resolve the path to module.xml.
I've changed the steps to those below and it worked:

module add --name=org.postgresql --slot=main --resources=/usr/local/lib/postgresql-9.4-1201.jdbc4.jar --dependencies=javax.api,javax.transaction.api

我遇到了与 WildFly 9 相同的错误和行为。我是 WF 的完全新手,但经过一些研究,我发现问题出在模块命名上。如果我做得很好,模块中的实际包名称将用于解析 module.xml 的路径。
我已将步骤更改为以下步骤并且它起作用了:

module add --name=org.postgresql --slot=main --resources=/usr/local/lib/postgresql-9.4-1201.jdbc4.jar --dependencies=javax.api,javax.transaction.api

/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgresql",driver-class-name=org.postgresql.Driver)

回答by Dentka Pawel M

Put your Postgres JDBC driver into deployment folder (just deploy). Now use CLI console and enter this command:

将您的 Postgres JDBC 驱动程序放入部署文件夹(只需部署)。现在使用 CLI 控制台并输入以下命令:

data-source add --name=PostgresqlDS --jndi-name=java:jboss/datasources/PostgresqlDS --driver-name=postgresql-9.4-1201.jdbc41.jar --connection-url=jdbc:postgresql://localhost:5432/test --user-name=USER --password=PASSWORD

Check if your driver is jdbc4.

检查您的驱动程序是否是 jdbc4。

I don't know why but adding datasources by web console doesn't work. By CLI works.

我不知道为什么,但是通过 Web 控制台添加数据源不起作用。通过 CLI 工作。

The right solution for extending JDBC drivers is add driver as module to server. In WildFly 9 you can do it using cli console. You can't do this by copy JDBC jar file (with xml) to "module" folder like in WildFly 8.

扩展 JDBC 驱动程序的正确解决方案是将驱动程序作为模块添加到服务器。在 WildFly 9 中,您可以使用 cli 控制台来完成。您不能通过将 JDBC jar 文件(带有 xml)复制到 WildFly 8 中的“module”文件夹来做到这一点

Execute commands:

执行命令:

module add --name=org.postgres --resources=postgresql-9.4-1201.jdbc41.jar --dependencies=javax.api,javax.transaction.api    

/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver)

To list instaled drivers enter:

要列出已安装的驱动程序,请输入:

/subsystem=datasources:installed-drivers-list

With driver creating datasources will be easy.

使用驱动程序创建数据源将很容易。

Please use 9.0 Final version. In CR are bugs.

请使用 9.0 最终版。在 CR 中是错误。

Regards, Pawel M

问候, 帕维尔 M

回答by Mircea Stanciu

I am running wildfly 10 in a docker:

我在 docker 中运行 wildfly 10:

#ADD DATASOURCES
RUN mkdir -p $JBOSS_HOME/modules/org/postgres/main
COPY files/postgresql-9.4.1208.jre7.jar $JBOSS_HOME/modules/org/postgres/main/
COPY files/module.xml $JBOSS_HOME/modules/org/postgres/main/
COPY files/standalone.xml $JBOSS_HOME/standalone/configuration

Where module.xml is

module.xml 在哪里

<module xmlns="urn:jboss:module:1.1" name="org.postgres">
    <resources>
        <resource-root path="postgresql-9.4.1208.jre7.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
        <module name="javax.servlet.api" optional="true"/>
    </dependencies>
</module>

And standalone contains driver:

并且独立包含驱动程序:

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

then datasource can be:

那么数据源可以是:

<datasource jndi-name="java:jboss/datasources/PostgresDS" pool-name="PostgresDS" enabled="true" use-java-context="true">
    <connection-url>jdbc:postgresql://ndis-db:5432/postgres</connection-url>
        <driver>postgresql</driver>
        ...

Note that my ndis-db is a postgres docker. In your case can be localhost.

请注意,我的 ndis-db 是一个 postgres docker。在你的情况下可以是本地主机。

How I ended up with the error mentioned by you: 1. file name spelled wrongly 2. /modules/org ...etc contain a typo 3. module.xml misspelled as modules.xml 4. ...

我是如何结束您提到的错误的:1. 文件名拼写错误 2. /modules/org ...等包含拼写错误 3. module.xml 拼写错误为 modules.xml 4. ...

回答by Sai prateek

Its very simple but could take more time if you will be new with JBOSS EAP/WilFlyUse below steps to create a datasource:

它非常简单,但如果您是JBOSS EAP/WilFly 的新手,可能需要更多时间 使用以下步骤创建数据源:

  1. Go to bin folder of server where jboss-cli(Power script) file present: right click on jboss-cli(power script file)--> Run with power shell(a console will open).

  2. Add the PostgreSQL JDBC driver as a core module.

  1. 转到 jboss-cli(Power 脚本)文件所在的服务器的 bin 文件夹:( right click on jboss-cli(power script file)--> Run with power shell将打开一个控制台)。

  2. 添加 PostgreSQL JDBC 驱动程序作为核心模块

module add --name=com.postgresql --resources=/path/to/postgresql-9.3-1102.jdbc4.jar --dependencies=javax.api,javax.transaction.api

模块添加 --name=com.postgresql --resources=/path/to/postgresql-9.3-1102.jdbc4.jar --dependencies=javax.api,javax.transaction.api

  1. Register the PostgreSQL JDBC driver.
  1. 注册 PostgreSQL JDBC 驱动程序。

/subsystem=datasources/jdbc-driver=postgresql:add(driver-name=postgresql,driver-module-name=com.postgresql,driver-xa-datasource-class-name=org.postgresql.xa.PGXADataSource)

/subsystem=datasources/jdbc-driver=postgresql:add(driver-name=postgresql,driver-module-name=com.postgresql,driver-xa-datasource-class-name=org.postgresql.xa.PGXADataSource)

  1. Add the PostgreSQL datasource.
  1. 添加 PostgreSQL 数据源。

data-source add --name=PostgresDS --jndi-name=java:jboss/PostgresDS --driver-name=postgresql --connection-url=jdbc:postgresql://localhost:5432/postgresdb --user-name=admin --password=admin --validate-on-match=true --background-validation=false --valid-connection-checker-class-name=org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker --exception-sorter-class-name=org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter

数据源添加 --name=PostgresDS --jndi-name=java:jboss/PostgresDS --driver-name=postgresql --connection-url=jdbc:postgresql://localhost:5432/postgresdb --user-name= admin --password=admin --validate-on-match=true --background-validation=false --valid-connection-checker-class-name=org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker - -exception-sorter-class-name=org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter

be careful with path path/tothis is path where your downloded Postgresql-jdbc.jaris present.

小心路径path/tothis 是您下载的Postgresql-jdbc.jar所在的路径。

回答by Stephen

You don't mention your java/jdbc version. I've just experienced the same issue and it was due to a driver vs Java 1.8 mismatch. With the wildfly 9 upgrade did you also upgrade Java?

你没有提到你的 java/jdbc 版本。我刚刚遇到了同样的问题,这是由于驱动程序与 Java 1.8 不匹配造成的。随着 wildfly 9 升级,您是否也升级了 Java?

The ".jdbc41." driver version is built for Java 1.7. Postgres has a matrix showing the combinations of Java/JDBC and Postgres driver versions that are compatible. Perhaps you need: postgresql-9.4.1209.jar (which is for 1.8/jdbc42)

“.jdbc41”。驱动程序版本是为 Java 1.7 构建的。Postgres 有一个矩阵,显示了兼容的 Java/JDBC 和 Postgres 驱动程序版本的组合。也许您需要:postgresql-9.4.1209.jar(用于 1.8/jdbc42)

Then in the CLI (assuming domain mode and profile=full)

然后在 CLI 中(假设域模式和配置文件=完整)

module add --name=org.postgresql.Driver --resources=/tmp/postgresql-9.4.1209.jar
connect
/profile=full/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgresql.Driver",driver-class-name=org.postgresql.Driver)
exit

回答by Francesco Marchioni

Just a note: I have tested the CLI commands, as taken from the tutorialmentioned, against WildFly 10 and it works correctly in creating the JDBC Driver and the datasource. Besides it, I can see that the error log reported contains a mispelling of the module name ("org.portgres"):

请注意:我已经针对 WildFly 10测试了来自上述教程的 CLI 命令,并且它在创建 JDBC 驱动程序和数据源时可以正常工作。除此之外,我可以看到报告的错误日志包含模块名称的拼写错误(“org.portgres”):

{
"outcome" => "failed",
"failure-description" => "WFLYJCA0041: Failed to load module for driver [org.portgres]",
"rolled-back" => true
}

Disclaimer: I'm the owner of mastertheboss.com

免责声明:我是 mastertheboss.com 的所有者