Java 带有数据源的 Wildfly 配置

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

Wildfly configuration with DataSource

javaweb-servicesdependenciesjndiwildfly

提问by Ilhami

this is the first time I am trying to setup datasource in my Wildfly server. I tried to follow some tutorials which I found on Google but it still doesn't work.

这是我第一次尝试在 Wildfly 服务器中设置数据源。我尝试按照我在 Google 上找到的一些教程进行操作,但仍然无效。

I am working on a web service but I keep getting some errors when I deploy my .war file.

我正在开发一个 Web 服务,但在部署 .war 文件时我不断收到一些错误。

Here is the latest log when app is deployed:

这是部署应用程序时的最新日志:

22:16:33,049 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015877: Stopped deployment IslamicPostsWS.war (runtime-name: IslamicPostsWS.war) in 7ms
22:16:33,184 INFO  [org.jboss.as.server] (XNIO-1 task-2) JBAS018558: Undeployed "IslamicPostsWS.war" (runtime-name: "IslamicPostsWS.war")
22:16:33,186 INFO  [org.jboss.as.controller] (XNIO-1 task-2) JBAS014774: Service status report
JBAS014777:   Services which failed to start:      service jboss.deployment.unit."IslamicPostsWS.war".POST_MODULE

22:16:35,518 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-6) JBAS015877: Stopped deployment IslamicPostsWS (runtime-name: IslamicPostsWS) in 7ms
22:16:35,660 INFO  [org.jboss.as.server] (XNIO-1 task-6) JBAS018558: Undeployed "IslamicPostsWS" (runtime-name: "IslamicPostsWS")
22:16:38,358 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 2) JBAS015018: Deployment IslamicPostsWS was previously deployed by this scanner but has been removed from the server deployment list by another management tool. Marker file C:\Users\Ilhami\workspace-services\.metadata\.plugins\org.jboss.ide.eclipse.as.core\WildFly_8.0_Runtime_Server1396040937545\deploy\IslamicPostsWS.undeployed is being added to record this fact.
22:17:00,406 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) JBAS015876: Starting deployment of "IslamicPostsWS.war" (runtime-name: "IslamicPostsWS.war")
22:17:00,540 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "IslamicPostsWS.war")]) - failure description: {
    "JBAS014771: Services with missing/unavailable dependencies" => ["jboss.naming.context.java.module.IslamicPostsWS.IslamicPostsWS.DefaultDataSource is missing [jboss.naming.context.java.jboss.datasources.ExampleDS]"],
    "JBAS014879: One or more services were unable to start due to one or more indirect dependencies not being available." => {
        "Services that were unable to start:" => [
            "jboss.deployment.unit.\"IslamicPostsWS.war\".component.\"com.sun.faces.config.ConfigureListener\".START",
            "jboss.deployment.unit.\"IslamicPostsWS.war\".component.\"javax.faces.webapp.FacetTag\".START",
            "jboss.deployment.unit.\"IslamicPostsWS.war\".component.\"javax.servlet.jsp.jstl.tlv.PermittedTaglibsTLV\".START",
            "jboss.deployment.unit.\"IslamicPostsWS.war\".component.\"javax.servlet.jsp.jstl.tlv.ScriptFreeTLV\".START",
            "jboss.deployment.unit.\"IslamicPostsWS.war\".component.\"org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher\".START",
            "jboss.deployment.unit.\"IslamicPostsWS.war\".component.\"org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap\".START",
            "jboss.deployment.unit.\"IslamicPostsWS.war\".deploymentCompleteService",
            "jboss.deployment.unit.\"IslamicPostsWS.war\".jndiDependencyService",
            "jboss.naming.context.java.module.IslamicPostsWS.IslamicPostsWS.env.jdbc.TestDB",
            "jboss.undertow.deployment.default-server.default-host./IslamicPostsWS",
            "jboss.undertow.deployment.default-server.default-host./IslamicPostsWS.UndertowDeploymentInfoService"
        ],
        "Services that may be the cause:" => [
            "jboss.jdbc-driver.com_mysql_jdbc_Driver",
            "jboss.naming.context.java.jboss.datasources.ExampleDS"
        ]
    }
}
22:17:00,683 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "IslamicPostsWS.war" (runtime-name : "IslamicPostsWS.war")
22:17:00,683 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 2) JBAS014774: Service status report
JBAS014775:    New missing/unsatisfied dependencies:
      service jboss.naming.context.java.jboss.datasources.ExampleDS (missing) dependents: [service jboss.naming.context.java.module.IslamicPostsWS.IslamicPostsWS.DefaultDataSource]

persistence.xml

持久化文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="JPADB">
        <jta-data-source>java:jboss/datasources/DBTest</jta-data-source>
        <properties>
            <property name="showSql" value="true"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        </properties>
    </persistence-unit>
</persistence>

Just tell me if you need more files.

如果您需要更多文件,请告诉我。

采纳答案by shillner

can you post your datasource definition?

你能发布你的数据源定义吗?

I think it would be the best to test the datasource deployment 'standalone'. I mean separated from an actual application deployment, just to test whether your datasource works or not. You can test this f.i. using the web console (localhost:9990/console).

我认为最好测试数据源部署“独立”。我的意思是与实际应用程序部署分开,只是为了测试您的数据源是否有效。您可以使用 Web 控制台 (localhost:9990/console) 测试此 fi。

It also looks like there are problems with the pre-configured example DS of wildfly. Did you remove this DS? In standalone.xml there is also a reference on ExampleDS which might be broken.

看起来wildfly的预配置示例DS也存在问题。您是否删除了此 DS?在standalone.xml 中还有一个对ExampleDS 的引用,它可能被破坏了。

回答by Alex Petty

Assuming you are using a MySQL DB, you can create a DS in the following way:

假设您使用的是 MySQL DB,您可以通过以下方式创建 DS:

(1) Download the mysql driver from here: http://dev.mysql.com/downloads/connector/j/

(1) 从这里下载 mysql 驱动程序:http: //dev.mysql.com/downloads/connector/j/

(2) Copy the mysql driver to: WILDFLY_HOME/modules/system/layers/base/com/mysql/main

(2)将mysql驱动复制到:WILDFLY_HOME/modules/system/layers/base/com/mysql/main

(3) From the JBoss (or Wildfly) console, run the command:

(3) 从 JBoss(或 Wildfly)控制台,运行命令:

/subsystem=datasources/jdbc-driver=mysql:add(driver-name=mysql, driver-module-name=com.mysql, driver-class-name=com.mysql.jdbc.Driver)

(4) Then, again from the JBoss (or Wildfly) console, run command:

(4) 然后,再次从 JBoss(或 Wildfly)控制台,运行命令:

/subsystem=datasources/data-source=YourDS:add(driver-name=mysql, user-name=USERNAME, password=PASSWORD, connection-url=jdbc:mysql://localhost:3306/YOURDATABASE, min-pool-size=5, max-pool-size=15, jndi-name=java:jboss/datasources/YourDS, enabled=true, validate-on-match=true, valid-connection-checker-class-name=org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker, exception-sorter-class-name=org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter)

Voila. Now you have a Datasource with a JNDI path of: java:jboss/datasources/YourDS

瞧。现在你有一个 JNDI 路径的数据源:java:jboss/datasources/YourDS

回答by Marek Gregor

Check if default-bindings configuration references to correct datasource: https://docs.jboss.org/author/display/WFLY8/Default+EE+Bindings+Configuration

检查默认绑定配置是否引用了正确的数据源:https: //docs.jboss.org/author/display/WFLY8/Default+EE+Bindings+Configuration

回答by Sam Sol

I had

我有

service jboss.naming.context.java.jboss.datasourservice jboss.naming.context.java.jboss.datasources.ExampleDS (missing) dependents:...

service jboss.naming.context.java.jboss.datasourservice jboss.naming.context.java.jboss.datasources.ExampleDS(缺失)依赖:...

on wildfly-8.1.0.Final.

在 Wildfly-8.1.0.Final 上。

IMHO

恕我直言

in standalone/configuration/standalone-full.xmlit had

standalone/configuration/standalone-full.xml它有

<default-bindings ... datasource="java:jboss/datasources/ExampleDS" ...

<default-bindings ... datasource="java:jboss/datasources/ExampleDS" ...

and

<datasource jndi-name="java:/datasources/ExampleDS"...

<datasource jndi-name="java:/datasources/ExampleDS"...

jndi-name mismatch!

jndi-name 不匹配!

I created ExampleDS2 with jndi-name="java:jboss/datasources/ExampleDS". This solved my problem.

我用 jndi-name="java:jboss/datasources/ExampleDS" 创建了 ExampleDS2。这解决了我的问题。

回答by user3402489

Go into your wildfly console (something like localhost:9990) and edit the Configuration->Container->EE->Default Bindings section. Set the default datasource to the JNDI name of some valid datasource.

进入您的 wildfly 控制台(类似于 localhost:9990)并编辑 Configuration->Container->EE->Default Bindings 部分。将默认数据源设置为某个有效数据源的 JNDI 名称。

I removed the default one that was there and just pointed it to my main datasource since the server exists to run one app only.

我删除了那里的默认值,并将其指向我的主数据源,因为该服务器仅用于运行一个应用程序。

回答by Nabil M

You probably have the ds declaration "java:jboss/datasources/ExampleDS" missing in your standalone.xml xmlns "urn:jboss:domain:datasources:2.0" section, just and it will work (here's the example from the default configuration):

您的 standalone.xml xmlns "urn:jboss:domain:datasources:2.0" 部分中可能缺少 ds 声明 "java:jboss/datasources/ExampleDS",它会起作用(这是默认配置中的示例):

            <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>

Or you can also add through the UI as mentioned by other response.

或者您也可以通过其他响应中提到的 UI 添加。