I want to do a JNDI lookup of a data source using the @Resource annotation. When I use the string "java:comp/env/demoDS" or "demoDS" it does not work. But if I use the lookup() method of the SessionContext and pass the name "demoDS", it works perfectly. Why doesn't the JNDI lookup work? The code is shown below.
I have read that resource injection works at both class level and field level. If the same resource is required in multiple fields, we might choose class level injection. So why isn't it working for me when I use a JNDI lookup? Like I said, it does work when I use the lookup() method of SessionContext.
Do what Jaikiran Pai suggested. If it works, it meant some how your resource was not declared under java name space. In that case, remove java name space from annotation
Otherwise, you can loop through and print whatever bound to the initial context to detect the JNDI name for your datasource.
Joined: Jan 17, 2005
Thanks, Jai Kiran and Vu Lee. When I changed the code as below, it works.
Can you explain why this JNDI name is not bound to the java:comp/env namespace? What do I need to do to bind the JNDI name of the data source to java:comp/env? Also, what is the code that I can use to loop and print out all the JNDI bindings?