5 Replies Latest reply on Nov 11, 2010 9:20 AM by Viji V

    jboss-6.0.0.20100911-M5 and Spring application context Jndi look up issue.

    Viji V Newbie

      jboss-6.0.0.20100911-M5 and Spring application context Jndi look up issue.

      I was looking into upgrading our Jboss envrionment to Jboss6. But noticed that application context fails on creating bean that does a jndi lookup using the comp/env, but it works just fine in JBoss 5.

      1. The following are my entries in the app context.

      <jee:jndi-lookup id="dataSource" jndi-name="java:jdbc/fleetcycle"/>  - This works in Jboss 5 and Jboss 6

      <jee:jndi-lookup id="dataSource1" jndi-name="java:comp/env/jdbc/fleetcycle"/> - This does not in Jboss 6, but works in Jboss 5. The exception I get is  Caused by: javax.naming.NameNotFoundException: env not bound

          I have entries defined in web.xml and jboss-web.xml for this


         
      web.xml
           <resource-ref id="ResourceRef_8">
        <description>Reference to JDBC data source for FleetCycle database</description>
        <res-ref-name>jdbc/fleetcycle</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
           </resource-ref>

      jboss-web.xml

           <resource-ref>
        <res-ref-name>jdbc/fleetcycle</res-ref-name>
        <jndi-name>java:/jdbc/fleetcycle</jndi-name>
           </resource-ref>
        


      2. The war also shows that defining an env-entry also does not work when I do the comp/env lookup

           <env-entry>
        <env-entry-name>module-name</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>xp</env-entry-value>
           </env-entry>

           <jee:jndi-lookup id="moduleName"
         jndi-name="java:comp/env/module-name"
         expected-type="java.lang.String"
         lookup-on-startup="true"/>   
             
      The exception I get is shown below. I have attached my testspring.war. I am able to look up using the comp/env entries from a JSP page without any issues in another application. Has any one encountered this problem? Do you have a fix for it? Or am I doing something wrong. Not sure. The same war works on Jboss 5 but does not work on JBoss 6.

      Caused by: javax.naming.NameNotFoundException: env not bound
        at org.jnp.server.NamingServer.getBinding(NamingServer.java:771) [:5.0.5.Final]
        at org.jnp.server.NamingServer.getBinding(NamingServer.java:779) [:5.0.5.Final]
        at org.jnp.server.NamingServer.getObject(NamingServer.java:785) [:5.0.5.Final]
        at org.jnp.server.NamingServer.lookup(NamingServer.java:396) [:5.0.5.Final]
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:728) [:5.0.5.Final]
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:835) [:5.0.5.Final]
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:688) [:5.0.5.Final]
        at javax.naming.InitialContext.lookup(InitialContext.java:392) [:1.6.0_22]
        at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154) [:3.0.3.RELEASE]
        at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) [:3.0.3.RELEASE]
        at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152) [:3.0.3.RELEASE]
        at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) [:3.0.3.RELEASE]
        at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) [:3.0.3.RELEASE]
        at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) [:3.0.3.RELEASE]
        at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201) [:3.0.3.RELEASE]
        at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187) [:3.0.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469) [:3.0.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409) [:3.0.3.RELEASE]
        ... 91 more

        • 1. Re: jboss-6.0.0.20100911-M5 and Spring application context Jndi look up issue.
          Viji V Newbie


           

          Has anyone run into this issue? How did you solve it? Please let me know.
          • 2. Re: jboss-6.0.0.20100911-M5 and Spring application context Jndi look up issue.
            jaikiran pai Master

            Looks like the Spring bean is being initialized even before the web application deployment's ENC (java:comp/env) is being setup. I don't know much about Spring, but I think you should some how add a dependency so that those Spring beans are initialized after the ENC is setup.

            • 3. Re: jboss-6.0.0.20100911-M5 and Spring application context Jndi look up issue.
              Viji V Newbie

              Jaikiran,

               

              Thank you very much for your reply. Spring beans are initialized by a listener which implements javax.servlet.ServletContextListener, the beans get loaded in the contextInitialized(). So based on that, what i did is I tried to see if I write a custom Listener which extends ServletContextListener will i be able to look up objects from the ENC in the contextInitialized method ? And my finding was that, in JBoss 5 I am able to and in JBoss 6 M5 I am unable to.

               

              This is my code for the servlet context listener

               

               

              ----------------------------------------------------------------------------------------------------------------------------------------------------


              package com.test;

              import javax.naming.InitialContext;
              import javax.naming.NamingException;
              import javax.servlet.ServletContextEvent;
              import javax.servlet.ServletContextListener;
              import javax.sql.DataSource;

              public class JBossServletContextListener implements ServletContextListener
              {

                  public void contextDestroyed(ServletContextEvent arg0)
                  {
                      System.out.println("Going to destroy");
                     
                  }

                  public void contextInitialized(ServletContextEvent arg0)
                  {
                      System.out.println("App Context Initialized");
                      InitialContext context;
                      try
                      {
                          context = new InitialContext();
                          DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/fleetcycle");
                          System.out.println("jndi look up from ENC successful");
                      } catch (NamingException e)
                      {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                     
                  }

              }


              ----------------------------------------------------------------------------------------------------------------------------------------------------


              web.xml has

               

              <?xml version="1.0" encoding="UTF-8"?>

              <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                       http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

              <welcome-file-list>
                <welcome-file>
                    index.jsp
                  </welcome-file>
              </welcome-file-list>
              <listener>
                <listener-class>com.test.JBossServletContextListener</listener-class>
              </listener>

              <resource-ref id="ResourceRef_0">
                <description>Reference to JDBC data source for FleetCycle database</description>
                <res-ref-name>jdbc/fleetcycle</res-ref-name>
                <res-type>javax.sql.DataSource</res-type>
                <res-auth>Container</res-auth>
                <res-sharing-scope>Shareable</res-sharing-scope>
              </resource-ref>

              <env-entry>
                <env-entry-name>module-name</env-entry-name>
                <env-entry-type>java.lang.String</env-entry-type>
                <env-entry-value>xp</env-entry-value>
              </env-entry>

              </web-app>

               

               

               

              Looks like the ServletContextListener.contextInitialized() gets called even before the application deployment's ENC is set up. THis used to be not the case in JBoss5. The .war that I used to test this is attached and it works in JBoss 5  and does not work in JBoss 6

               

              In Jboss 5. My system out print outs work good.

              14:10:09,277 INFO  [TomcatDeployment] deploy, ctxPath=/jbossServletListener
              14:10:09,317 INFO  [STDOUT] App Context Initialized
              14:10:09,317 INFO  [STDOUT] jndi look up from ENC successful

               

              In Jboss 6, it is unable to look up the jndi instance

              14:11:47,400 INFO  [TomcatDeployment] deploy, ctxPath=/jbossServletListener
              14:11:47,420 INFO  [STDOUT] App Context Initialized

              14:11:47,430 ERROR [STDERR] javax.naming.NameNotFoundException: env not bound

              14:11:47,430 ERROR [STDERR]  at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)

               

              Anyone knows why the contextInitialized function gets called without the envrionment completely set up for the application? Any solution for this?

              • 4. Re: jboss-6.0.0.20100911-M5 and Spring application context Jndi look up issue.
                jaikiran pai Master

                Viji V wrote:

                 

                Looks like the ServletContextListener.contextInitialized() gets called even before the application deployment's ENC is set up. THis used to be not the case in JBoss5. The .war that I used to test this is attached and it works in JBoss 5  and does not work in JBoss 6

                 

                ...


                Anyone knows why the contextInitialized function gets called without the envrionment completely set up for the application? Any solution for this?

                It's one of the issues that we are trying to solve before AS 6 goes final. We have made some good progress and this will be fixed some time soon. The work-in-progress design can be found in these articles:

                 

                http://community.jboss.org/wiki/Switchboard

                http://community.jboss.org/wiki/DesignofJBossInjection