1
所以我使用Wildfly 8.2.0.Final在persistence.xml中使用JNDI名稱引用數據源
我成功創建了我的MySQL數據源。證明:從管理控制檯,我做「測試連接」,它會給出
成功連接到數據庫MyAppDS。
在獨立-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