1 Reply Latest reply on Oct 8, 2015 11:52 AM by sjunejo

    EJB fails to lookup JDBC DataSource res-ref from code

    sjunejo

      I have an auto-start EJB tested with jBoss EAP 6.[3,4] and it fails to lookup JDBC resource ref defined in deployment descriptor with exception;

       

      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

       

      AutoStart EJB looks like as follows;

       

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

       

      The interesting fact is that deployment works fine...i.e. my EJB is successfully deployed with no errors which means it jBoss Application Server successfully found the DataSource java:/jdbc/myDS but when I try to access the same in my auto-start code it fails! Also just to add here, if I directly lookup JNDI (which is not desired because it won't work on WebSphere, WebLogic) it finds the resource OK.

       

      I have done this many times with other JCA resources and never faced a problem, I think I might have missed something which I am not able to pin point! Can you please help?