2
votes

We have a personal java library which is deployed on a tomcat server (v5.5 & v6). This library is shared with many web applications, so it was deployed on shared/lib in tomcat directory.

Exceptionaly, i have to create a new class into this library which lookup a jdbc datasource via JNDI.

For that, here my code :

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource dataSource = (DataSource) envCtx.lookup("jdbc/corp.xx.xx.xxxxDS");

All context are instantiated without problem. But when it lookup the jdbc datasource, i have an exception : Name jdbc not Found in Context

We are not in a webapp, is it a problem ? Is it possible to declare a context.xml with a ResourceLink ? how ?

nb: the jndi is setted in server.xml (a resource in GlobalNamingResources)

1
When you write 'pb' do you perhaps mean 'problem'? Please don't treat this place like a mobile phone.user207421

1 Answers

2
votes
<Context ...>
  ...
  <ResourceLink name="linkToGlobalResource"
            global="simpleValue"
            type="java.lang.Integer"
  ...
</Context>

Where name is the new name (name relative to java:comp/env), global is the global jndi name configured in server.xml

So, from your code, look up linkToGlobalResource which will give you the linked global resource

Reference: http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Resource_Links

Answer Updated:

Your JNDI access will be from web-app and not shared jar because standalone jar does not have the concept of context.