JNDI Lookup to java:comp/env results in 'javax.naming.NameNotFoundException: java:comp/env'

Latest response

Hi,

we use a third party application which can be enabled to use datasources of the application server for database access.
With JBoss 5.x the application worked fine using datasources which were configured within the JBoss instance.

After migration to JBoss EAP 6.2 EAP the application fails when trying to use a datasource.
The logfile of the application shows then the following entry:

16:50:31,856 INFO  [dbRequest] (pool-4-thread-1) isPooled: true
16:50:31,858 ERROR [dbRequest] (pool-4-thread-1) javax.naming.NameNotFoundException: java:comp/env
    at org.jboss.as.naming.InitialContext.findContext(InitialContext.java:198)
    at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:118)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:183)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:179)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at com.genesyslab.studio.backendlogic.db.CVDBBackendHandler.getConnection(Unknown Source)
    at com.genesyslab.studio.backendlogic.db.CVDBBackendHandler.access$100(Unknown Source)
    at com.genesyslab.studio.backendlogic.db.CVDBBackendHandler$1.call(Unknown Source)
    at com.genesyslab.studio.backendlogic.db.CVDBBackendHandler$1.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

When decompling the library they use for opening a datasource connection we found the following code:

                InitialContext initialContext = new InitialContext();
                Context context = (Context)initialContext.lookup("java:comp/env");
                DataSource dataSource = (DataSource)context.lookup(string5);
                logger.info((Object)"CVDBBackendHandler.getConnection() out");
                return dataSource.getConnection();

I could veryfy with a simple EAR that the following line causes "NameNotFoundException" in JBoss EAP 6.2 GA and JBoss 6.4 GA:

Context context = (Context)initialContext.lookup("java:comp/env");

We opened a ticket against the vendor, but he insists that the JNDI context "java:comp/env" still exists in 6.2 and 6.4 and rejected the ticket.
The interesting part is, that all RH EAP documentation we found, this JNDI context is still referenced for EAP 6.x.

So here are our questions:
a) if "java:comp/env" is still available, why get I a "NameNotFoundException" when I use 'Context context = (Context)initialContext.lookup("java:comp/env");'?
Do I have to change somethin in the "standalone.xml"?

b) if "java:comp/env" isn't supported any longer, where can I find an official statment, that it isn't any longer?

Best Regards
Peter

Responses

Hi

Take a look at the following Knowledge Base Article [1]. I'm a little confused cos you're not actually looking up the jndi name in the code snippet you've linked. You're just trying to lookup the namespace and not the jndi name of the datasource being deployed. Take a look at https://github.com/jasinner/redhat/blob/master/jndi-datasource/src/main/java/example/TestServlet.java for what I'm referring to.

[1] https://access.redhat.com/solutions/1408753

Hi,
thank you for your reply. The document you referenced I found already before, but it didn't work for me.

1) the code you see is from the library of the vendor. So I can't change here anything, as it's not our sourcode. The vendor only supports Tomcat, but our customer uses JBoss as middleware.

As I investigated the error on JBoss 6 (the vendors software worked on JB 5 well), I decompiled the Java library and found the lines of code I pasted above. When I run the application I see always that the application will fail at this line of code:

Context context = (Context)initialContext.lookup("java:comp/env");

So the application never will reach the point, where it tries to find the datasource.

2) I wrote a tiny test EAR, which also tries a 'initialContext.lookup("java:comp/env");' and I also see the 'NameNotFoundException' when I run my code.
At that point I'm a little bit confused. I saw in several JB 6 documents statements referencing "java:com/env" context.
So why fails the simple lookup to "java:comp/env" in JB 6.x?
At that point I don't speek about looking for a datasource.
BR
Peter

Hi, I have a similar problem on jboss eap6. I've tried to lookup "java:comp/BeanManager" and "java:comp/env/BeanManager" from @PostPersist and @EntityListeners but it always fails with NameNotFoundException. (lookup works from a "normal" sessionbean)

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.