4 Replies Latest reply on Apr 25, 2013 10:16 PM by beubanks

    JNDI datasource lookup not working from Quartz thread.

    beubanks Newbie

      I have an application that uses JNDI to lookup a datasource configured in my JBoss AS 7.1.3 server. It uses the datasource for running a report. When I run the report directly through the web interface (and thus in the thread that handles the HTTP requests), the JNDI lookup works fine. However, when the same report is run from a Quartz job, it throws the following error.

       

      javax.naming.NameNotFoundException: java:comp/env/jdbc/example

          at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:127)

          at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:215)

          at javax.naming.InitialContext.lookup(InitialContext.java:411)

          ...

       

      The code that does the lookup in the Quartz job is the same code that does the lookup for the manually-run report. (Based on the stack trace, although I don't have the original source.)

       

      To compare the behavior, I forced it to fail for the manual run by changing the JNDI name to an invalid name, and I got the following:

       

      javax.naming.NameNotFoundException: env/jdbc/invalidname -- service jboss.naming.context.java.module.examplewar.examplewar.env.jdbc.invalidname
          at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97)
          at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:179)
          at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:129)
          at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:215)
          at javax.naming.InitialContext.lookup(InitialContext.java:411)
          ...
      

       

      Notice the lack of "java:comp/" in the manually executed example. That's weird, because I decompiled the code using JD-GUI, and the line that calls InitialContext.lookup() explicitly prepends "java:comp/env/" like this:

       

      DataSource ds = (DataSource)this.ctx.lookup("java:comp/env/" + jndiName);
      

       

      So it looks to me like it's seeing the JNDI context differently in Quartz thread. Any ideas why it would do that, or what's really happening?

       

      Datasource in standalone.xml:

        <datasource jta="true" jndi-name="java:/jdbc/example" pool-name="exampleDS" enabled="true" use-java-context="true" use-ccm="true">
          ...
        </datasource>
      

       

      Datasouce configuration in web.xml:

      <?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">
      
          ...
          
          <resource-ref>
            <description>Example Database</description>
            <res-ref-name>jdbc/example</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
            <lookup-name>java:/jdbc/example</lookup-name>
            <res-sharing-scope>Shareable</res-sharing-scope>
          </resource-ref>
          
          ...
          
      </web-app>
      

       

      jboss-web.xml:

      <?xml version="1.0" encoding="UTF-8" ?>
      <jboss-web>
        <resource-ref>
          <description>Example Database</description>
          <res-ref-name>jdbc/example</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
          <lookup-name>java:/jdbc/example</lookup-name>
          <res-sharing-scope>Shareable</res-sharing-scope>
        </resource-ref>
      </jboss-web>
      

       

      Thanks,

      Bron