在persistence.xml中引用具有JNDI名称的数据源

时间:2014-12-06 14:24:47

标签: java mysql java-ee wildfly persistence.xml

所以我使用Wildfly 8.2.0.Final

我成功创建了MySQL数据源。证明:从管理控制台,我做“测试连接”,它给出了

  

已成功连接到数据库MyAppDS。

standalone-full.xml中的数据源定义:

    <datasources>
        <datasource jta="true" jndi-name="java:/jdbc/MyAppDS" pool-name="MyAppDS" enabled="true" use-ccm="true">
            <connection-url>jdbc:mysql://localhost:3306/myapp</connection-url>
            <driver-class>com.mysql.jdbc.Driver</driver-class>
            <driver>mysql</driver>
            <security>
                <user-name>root</user-name>
            </security>
            <validation>
                <validate-on-match>false</validate-on-match>
                <background-validation>false</background-validation>
            </validation>
            <timeout>
                <set-tx-query-timeout>false</set-tx-query-timeout>
                <blocking-timeout-millis>0</blocking-timeout-millis>
                <idle-timeout-minutes>0</idle-timeout-minutes>
                <query-timeout>0</query-timeout>
                <use-try-lock>0</use-try-lock>
                <allocation-retry>0</allocation-retry>
                <allocation-retry-wait-millis>0</allocation-retry-wait-millis>
            </timeout>
            <statement>
                <share-prepared-statements>false</share-prepared-statements>
            </statement>
        </datasource>
        <drivers>
            <driver name="mysql" module="com.mysql">
                <xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
            </driver>
        </drivers>
    </datasources>

我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
    <persistence-unit name="myapp_myapp_war_1.0PU" transaction-type="JTA">
        <jta-data-source>java:/jdbc/MyAppDS</jta-data-source>
        <class>org.myapp.entity.Place</class>
        <class>org.myapp.entity.User</class>
        <class>org.myapp.entity.MyCall</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        </properties>
    </persistence-unit>
</persistence>

部署应用程序之前的服务器日志显示

15:07:24,550 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-3) JBAS010400: Bound data source [java:/jdbc/MyAppDS]

当我调试我的应用程序显示以下内容时,出现以下行

15:09:13,175 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 1) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "myapp-1.0.war")]) - failure description: {"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.naming.context.java.module.\"myapp-1.0\".\"myapp-1.0\".DefaultDataSource is missing [jboss.naming.context.java.jboss.datasources.ExampleDS]"]}
15:09:13,222 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "myapp-1.0.war" (runtime-name : "myapp-1.0.war")
15:09:13,237 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 1) 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."myapp-1.0"."myapp-1.0".DefaultDataSource] 

15:09:21,177 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 2) JBAS014613: Operation ("read-attribute") failed - address: ([
    ("deployment" => "myapp-1.0.war"),
    ("subsystem" => "ejb3"),
    ("stateless-session-bean" => "MyCallFacadeREST")
]) - failure description: "JBAS014508: EJB component for address [
    (\"deployment\" => \"myapp-1.0.war\"),
    (\"subsystem\" => \"ejb3\"),
    (\"stateless-session-bean\" => \"MyCallFacadeREST\")
] is in 
 state DOWN, must be in state UP"
15:09:23,356 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-7) JBAS010418: Stopped Driver service with driver-name = myapp-1.0.war_com.mysql.jdbc.Driver_5_1
15:09:23,356 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) JBAS010418: Stopped Driver service with driver-name = myapp-1.0.war_com.mysql.fabric.jdbc.FabricMySQLDriver_5_1
15:09:23,356 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 60) JBAS011410: Stopping Persistence Unit (phase 2 of 2) Service 'myapp-1.0.war#myapp_myapp_war_1.0PU'
15:09:23,356 INFO  [org.jboss.weld.deployer] (MSC service thread 1-5) JBAS016009: Stopping weld service for deployment myapp-1.0.war
15:09:23,403 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 60) JBAS011410: Stopping Persistence Unit (phase 1 of 2) Service 'myapp-1.0.war#myapp_myapp_war_1.0PU'
15:09:23,434 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-3) JBAS015877: Stopped deployment myapp-1.0.war (runtime-name: myapp-1.0.war) in 81ms

是否发生这种情况是因为服务器找不到我指向的数据源,所以它试图找到默认数据源ExampleDS(我已从我的standalone-full.xml中删除了

1 个答案:

答案 0 :(得分:3)

当您删除ExampleDS数据源时,您是否也在ee子系统的默认绑定中更改了对它的引用?默认情况下,Wildfly有这个条目:

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

如果您不想保留ExampleDS,您可以将数据源设置为默认值,或者(如果我没记错的话)默认绑定是可选的,以便在必要时将其删除。