How to map jpa datasources in WildFly?

佐手、 提交于 2020-03-22 09:07:28

问题


I have an EJB 3.1 application with JPA, the @Stateless bean has an EntityManager and the persistence.xml is configured to use java:comp/env/jf/demo/ds JNDI for the DataSource.

In the ejb-jar.xml I have declared that my EJB needs the jf/demo/ds and in the ibm deployment descriptor I mapped jf/demo/ds to the real JNDI name of the DataSource. This is working on WebSphere, but I need to make it work also on WildFly 10 / JBoss EAP 7.

I've found some examples with the jboss.xml mapping file but WildFly requires a jboss-ejb3.xml and I cannot find any working example with the DataSource mapping.

My EJB:

@Stateless
public class CartPersistence extends PersistenceBean {

    @PersistenceContext(unitName = "jf-demo-persistence")
    protected EntityManager _em;
    ...
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/persistence"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="jf-demo-persistence">
        <jta-data-source>java:comp/env/jf/demo/ds</jta-data-source>
        <class>factory.jf.demo.persistence.CartEntity</class>
        <class>factory.jf.demo.persistence.ShopEntity</class>
    </persistence-unit>

</persistence>

ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         version="3.1"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
    <module-name>jf-demo-persistence</module-name>
    <enterprise-beans>
        <session>
            <ejb-name>CartPersistence</ejb-name>
            <resource-ref>
                <res-ref-name>jf/demo/ds</res-ref-name>
                <res-type>javax.sql.DataSource</res-type>
            </resource-ref>
        </session>
    </enterprise-beans>
</ejb-jar>

ibm-ejb-jar-bnd.xml (only for WAS)

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee
             http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_0.xsd" version="1.0">
    <session name="CartPersistence">
        <resource-ref name="jf/demo/ds" binding-name="jf/ds"/>
    </session>
</ejb-jar-bnd>

jboss-ejb3.xml (not working, tag is not present in the schema)

<?xml version="1.0" encoding="UTF-8"?>
<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
               xmlns="http://java.sun.com/xml/ns/javaee"
               xmlns:iiop="urn:iiop"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd
http://java.sun.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-spec-2_0.xsd
urn:iiop jboss-ejb-iiop_1_0.xsd"
               version="3.1"
               impl-version="2.0">
    <enterprise-beans>
        <session>
            <ejb-name>CartPersistence</ejb-name>
            <resource-ref>
                <res-ref-name>jf/demo/ds</res-ref-name>
                <jndi-name>java:/jf/ds</jndi-name>
            </resource-ref>
        </session>
    </enterprise-beans>
</jboss:ejb-jar>

standalone.xml

 <subsystem xmlns="urn:jboss:domain:datasources:4.0">
        <datasources>
            <datasource jndi-name="java:/jf/ds" pool-name="JfDS">
                <connection-url>jdbc:oracle:thin:@//****/****</connection-url>
                <driver>oracle</driver>
                <security>
                    <user-name>****</user-name>
                    <password>****</password>
                </security>
            </datasource>

When WildFly starts I get this error on the application deploy:

09:27:03,550 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 1) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "jf-demo-web.war")]) - failure description: {
    "WFLYCTL0412: Required services that are not installed:" => ["jboss.naming.context.java.module.jf-demo-web.jf-demo-web.env.jf.demo.ds"],
    "WFLYCTL0180: Services with missing/unavailable dependencies" => [
        "jboss.persistenceunit.\"jf-demo-web.war#jf-demo-persistence\".__FIRST_PHASE__ is missing [jboss.naming.context.java.module.jf-demo-web.jf-demo-web.env.jf.demo.ds]",
        "jboss.persistenceunit.\"jf-demo-web.war#jf-demo-persistence\" is missing [jboss.naming.context.java.module.jf-demo-web.jf-demo-web.env.jf.demo.ds]"
    ]
}

来源:https://stackoverflow.com/questions/42434282/how-to-map-jpa-datasources-in-wildfly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!