EJB 无法从代码中查找 JDBC DataSource res-ref

EJB fails to lookup JDBC DataSource res-ref from code

我有一个使用 jBoss EAP 6.[3,4] 测试的自动启动 EJB,但它无法查找部署描述符中定义的 JDBC 资源引用,异常;

Caused by: javax.ejb.EJBException: java.lang.RuntimeException: javax.naming.NameNotFoundException: env/jdbc/MyDS -- service jboss.naming.context.java.
comp."DFETL_EAR-16.0.0-SNAPSHOT"."df-extract-16.0.0-SNAPSHOT".ExtractStartupBean.env.jdbc.MyDS
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:191)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:282)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.requiresNew(CMTTxInterceptor.java:367)
        at org.jboss.as.ejb3.tx.LifecycleCMTTxInterceptor.processInvocation(LifecycleCMTTxInterceptor.java:66)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)


        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:70) [jboss-invocation-1.1.2.Final-r
edhat-1.jar:1.1.2.Final-redhat-1]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Fina
l-redhat-1]
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:162) [jboss-as-ee-7.5.0.Final-redhat-21.jar:7.5.0.F
inal-redhat-21]
        ... 12 more
Caused by: java.lang.RuntimeException: javax.naming.NameNotFoundException: env/jdbc/MyDS -- service jboss.naming.context.java.comp."DFETL_EAR-16.0.0-S
NAPSHOT"."df-extract-16.0.0-SNAPSHOT".ExtractStartupBean.env.jdbc.MyDS
        at com.temenos.technology.dataframework.extract.ExtractStartupBean.initialize(ExtractStartupBean.java:66)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_17]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_17]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_17]
        at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_17]
        at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptor.java:96)
[jboss-as-ee-7.5.0.Final-redhat-21.jar:7.5.0.Final-redhat-21]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-
redhat-1]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.as.ejb3.component.session.SessionBeanSetSessionContextMethodInvocationInterceptor.processInvocation(SessionBeanSetSessionContextM
ethodInvocationInterceptor.java:56)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptorFactory$ManagedReferenceFieldInjectionInterceptor.processInvocation(Mana
gedReferenceFieldInjectionInterceptorFactory.java:109) [jboss-as-ee-7.5.0.Final-redhat-21.jar:7.5.0.Final-redhat-21]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-
redhat-1]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.as.ee.component.ComponentInstantiatorInterceptor.processInvocation(ComponentInstantiatorInterceptor.java:76) [jboss-as-ee-7.5.0.F
inal-redhat-21.jar:7.5.0.Final-redhat-21]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-
redhat-1]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) [jboss-as-ee-7.5.0.Final-redha
t-21.jar:7.5.0.Final-redhat-21]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.2.Final-redhat-1.jar:1.1.2.Final-redhat-
1]
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:280)
        ... 21 more
Caused by: javax.naming.NameNotFoundException: env/jdbc/MyDS -- service jboss.naming.context.java.comp."DFETL_EAR-16.0.0-SNAPSHOT"."df-extract-16.0.0-
SNAPSHOT".ExtractStartupBean.env.jdbc.MyDS
        at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:104) [jboss-as-naming-7.5.0.Final-redhat-21.jar:7.5.0.Final
-redhat-21]
        at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:197) [jboss-as-naming-7.5.0.Final-redhat-21.jar:7.5.0.Final-redhat-21]
        at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:174) [jboss-as-naming-7.5.0.Final-redhat-21.jar:7.5.0.Final-redhat-21]
        at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:245) [jboss-as-naming-7.5.0.Final-redhat-21.jar:7.5.0.F
inal-redhat-21]
        at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:183) [jboss-as-naming-7.5.0.Final-redhat-21.jar:7.5.0.Final-redhat-21]
        at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:179) [jboss-as-naming-7.5.0.Final-redhat-21.jar:7.5.0.Final-redhat-21]
        at javax.naming.InitialContext.lookup(InitialContext.java:411) [rt.jar:1.7.0_17]
        at javax.naming.InitialContext.lookup(InitialContext.java:411) [rt.jar:1.7.0_17]
        at com.temenos.technology.dataframework.extract.ExtractStartupBean.initialize(ExtractStartupBean.java:62)
        ... 42 more


14:15:30,832 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) JBAS014612: Operation ("full-replace-deployment") fa
iled - address: ([]) - failure description: {"JBAS014671: Failed services" => {"jboss.deployment.subunit.\"DFETL_EAR-16.0.0-SNAPSHOT.ear\".\"df-extrac
t-16.0.0-SNAPSHOT.jar\".component.ExtractStartupBean.START" => "org.jboss.msc.service.StartException in service jboss.deployment.subunit.\"DFETL_EAR-1
6.0.0-SNAPSHOT.ear\".\"df-extract-16.0.0-SNAPSHOT.jar\".component.ExtractStartupBean.START: java.lang.IllegalStateException: JBAS011048: Failed to con
struct component instance
    Caused by: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
    Caused by: javax.ejb.EJBException: java.lang.RuntimeException: javax.naming.NameNotFoundException: env/jdbc/MyDS -- service jboss.naming.context.j
ava.comp.\"DFETL_EAR-16.0.0-SNAPSHOT\".\"df-extract-16.0.0-SNAPSHOT\".ExtractStartupBean.env.jdbc.MyDS
    Caused by: java.lang.RuntimeException: javax.naming.NameNotFoundException: env/jdbc/MyDS -- service jboss.naming.context.java.comp.\"DFETL_EAR-16.
0.0-SNAPSHOT\".\"df-extract-16.0.0-SNAPSHOT\".ExtractStartupBean.env.jdbc.MyDS
    Caused by: javax.naming.NameNotFoundException: env/jdbc/MyDS -- service jboss.naming.context.java.comp.\"DFETL_EAR-16.0.0-SNAPSHOT\".\"df-extract-
16.0.0-SNAPSHOT\".ExtractStartupBean.env.jdbc.MyDS"}}
14:15:30,864 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015974: Stopped subdeployment (runtime-name: df-extract-16.0.0-SNAPSH
OT.jar) in 9ms
14:15:30,864 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015877: Stopped deployment DFETL_EAR-16.0.0-SNAPSHOT.ear (runtime-nam
e: DFETL_EAR-16.0.0-SNAPSHOT.ear) in 15ms
14:15:30,864 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 2) JBAS014774: Service status report
JBAS014776:    Newly corrected services:
      service jboss.deployment.subunit."DFETL_EAR-16.0.0-SNAPSHOT.ear"."df-extract-16.0.0-SNAPSHOT.jar".moduleDeploymentRuntimeInformation (no longer
required)
JBAS014777:   Services which failed to start:      service jboss.deployment.subunit."DFETL_EAR-16.0.0-SNAPSHOT.ear"."df-extract-16.0.0-SNAPSHOT.jar".c
omponent.ExtractStartupBean.START

打包在 EAR 中的 AutoStart EJB 如下所示;

@Singleton  
@Startup  
public class ExtractStartupBean implements SessionBean {    
     @PostConstruct  
    public void initialize() {  

          try {  
               InitialContext context = new InitialContext();  
               DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/MyDS");  
               Connection conn = ds.getConnection();  
               System.out.println("JDBC Client Info : " + conn.getClientInfo());  
          } catch (Exception e) {  
               throw new RuntimeException(e);  
          }  
     }  

     @PreDestroy  
     public void terminate() {  
          // Do something....Not important right now...  
     }  
}  

ejb-jar.xml部署描述符

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.1"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">  
  <enterprise-beans>  
  <session>  
  <ejb-name>DFExtractor</ejb-name>  
  <ejb-class>com.temenos.technology.dataframework.extract.ExtractStartupBean</ejb-class>  
  <session-type>Stateless</session-type>  
  <resource-ref>  
  <description>JDBC Pool Of DS</description>  
  <res-ref-name>jdbc/myDS</res-ref-name>  
  <res-type>javax.sql.DataSource</res-type>  
  <res-auth>Container</res-auth>  
  </resource-ref>  
  </session>  
  </enterprise-beans>  
</ejb-jar>  

jboss-ejb3.xml部署描述符

<?xml version="1.0"?>  
<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"  
  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xmlns:p="urn:ejb-pool:1.0"  
  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://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"  
  version="3.1" impl-version="2.0">  
  <enterprise-beans>  
  <session>  
  <ejb-name>DFExtractor</ejb-name>  
  <resource-ref>  
  <res-ref-name>jdbc/myDS</res-ref-name>  
  <res-type>javax.sql.DataSource</res-type>  
  <jndi-name>java:/jdbc/myDS</jndi-name>  
  </resource-ref>  
  </session>  
  </enterprise-beans>  
</jboss:ejb-jar>  

有趣的是部署工作正常......即我的 EJB 已成功部署且没有错误,这意味着它 jBoss Application Server 已成功找到 DataSource java:/jdbc/myDS 但是当我尝试在我的自动启动代码中访问它时它失败了!还只是在这里添加,如果我直接查找 JNDI(这是不需要的,因为它不能在 WebSphere、WebLogic 上工作),它会找到资源 OK。

我已经用其他 JCA 资源做过很多次了,从来没有遇到过问题,我想我可能错过了一些我无法确定的东西!

您的 EJB session-type 不是 Stateless,而是 Singleton:

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.1"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">  
  <enterprise-beans>  
      <session>  
          <ejb-name>DFExtractor</ejb-name>  
          <ejb-class>com.temenos.technology.dataframework.extract.ExtractStartupBean</ejb-class>  
          <session-type>Singleton</session-type>
          <init-on-startup>true</init-on-startup>  
          <resource-ref>  
              <description>JDBC Pool Of DS</description>  
              <res-ref-name>jdbc/myDS</res-ref-name>  
              <res-type>javax.sql.DataSource</res-type>  
              <res-auth>Container</res-auth>  
          </resource-ref>  
      </session>  
  </enterprise-beans>  
</ejb-jar>

此外,您需要匹配您的 ejb 名称:

@Singleton(name="DFExtractor")
@Startup  
public class ExtractStartupBean implements SessionBean {    

也就是说,如果您添加:

@Resource(name="jdbc/myDS")
private DataSource ds;

到您的 EJB 然后您将不再需要 ejb-jar.xml 文件。您仍然可以使用 jboss-ejb3.xml 来可移植地映射您的 JNDI 资源。