I configured my web application to access the database using WebSphere
JDBC provider.
It works for all servlets except one that is loaded on startup an get
the following error:
javax.naming.NameNotFoundException: Name comp/env/jdbc not found in
context "java:".
at
com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1663)
at
com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1009)
at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:932)
at
com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(UrlContextImpl.java:1261)
at
com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:194)
at
com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:135)
at javax.naming.InitialContext.lookup(InitialContext.java:360)
All other servlets work well and if I remove the load-on-startup option
in web.xml and manually call the same servlet it works without any problem.
Hints?
Thanks.
A WebSphere newbie.
Do you perform your jndi lookup from an application created thread
or from the Websphere created thread ?
It is not possible to lookup "java:comp/env/" namespace from an
application created thread.
Cyrille
--
Cyrille Le Clerc
cyrille...@pobox.com
cyrille...@fr.ibm.com
Yes, it is an application created thread. Why there is such limitation?
Is it a policy setting? Can I change it?
Can you point me to some documentation about this?
Again, thank you very much.
A WebSphere newbie.
Thanks
A WebSphere newbie.
WHY LOOKUPS ON "JAVA:COMP/ENV" DON'T WORK ON APPLICATION CREATED THREAD
?
Your problem is a "work as design" feature of Websphere.
Websphere does not allow to do a "java:comp/env/..." jndi lookup
from an application created thread.
All this is explained in two technotes :
- Technote 1168795 : "NameNotFoundException when an EJB lookup is
requested"
-> http://www-1.ibm.com/support/docview.wss?uid=swg21168795
- Technote 1168795 : "InitialContext in a servlet or JSP, threads
created by the servlet or JSP fail with a noThreadContext message and a
nullPointerException."
-> http://www-1.ibm.com/support/docview.wss?uid=swg21175742
Here is an extract of a technote :
"Do not execute the look-up in a separate thread. There is no way to
provide a spun-off thread in any context. So, generally speaking, the
code must be modified so that it does not do the java: look-up from a
thread that you spin off."
WORKAROUNDS
* The main workaround is refactor your code and perform the jndi lookup
on a Websphere Created thread (ie Servlet or ServletContextListener
thread).
- Pros :
-> Websphere's best practice
- Cons :
-> code refactoring can be important
* Another workaround, but an extremely inadvisable one, is to jndi
lookup on "jdbc/myDataSource" without the "java:comp/env" prefix.
- Pros :
-> no code refactoring, just a change of the config config
- Cons :
-> in Websphere 6+, logs will be flooded by "J2CA0294W:
Deprecated usage of direct JNDI lookup of resource"
-> you loose the "<res-sharing-scope>" behavior that is defined
in you web.xml (or ejb-jar.xml)
* Yet another workaround, switch to an external production ready
DataSource (e.g. Jakarta DBCP, C3P0, ...)
- Pros :
-> simple data sources are simple to configure and may fit your
requirements (perf, config, ...)
- Cons :
-> You may want to manage your data source in the application
server config
-> you loose Websphere monitoring (Tivoli Performance Viewer)
Hope this helps,